0

I have created a treewidget and I have added some treewidgetitems which is editable.

My goal is now to catch the new value of the item after entering it.

Here is the code:

    QTreeWidgetItem* child = new QTreeWidgetItem();
    child->setText(0, "New Folder");
    child->setText(1, "--");
    child->setText(2, "--");
    child->setFlags(child->flags() | Qt::ItemIsEditable);
    item[0]->addChild(child);
    item[0]->setExpanded(true);
    MyTree->editItem(child);

    MyTree->setCurrentItem(child);
    ...

When "editItem" is set, I can on the interface enter manuel the new value. What I need is to be able the catch the new value after I press "enter" key. When the item become editable the name is "New Folder", the text is selected and I enter a new name such as "blabla" and press enter.

I need to catch the "blabla". I have tried using the code below:

    ....
    MyTree->setCurrentItem(child);

    QList<QTreeWidgetItem *> item;

    item = MyTree->selectedItems();
    QString str = item[0]->text(0);
    QByteArray latin_str = str.toLatin1();
    char *utf8_text = latin_str.data();

but the utf8_text report "New Folder" instead of "blabla"

Any idea ?

Seb
  • 2,929
  • 4
  • 30
  • 73

2 Answers2

0

You need to create a class derived from QObject, and listen to the QTreeWidget's itemChanged signal. For example:

class MyWidget : public QWidget
{
    Q_OBJECT;
public:
    MyWidget(QWidget * parent = nullptr) : QWidget(parent)
    {
        // Create sub-widgets, including m_tree

        // Connect up signal and slot
        connect(&m_tree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(treeItemChanged(QTreeWidgetItem*, int)));
    }

public slots:
    void treeItemChanged(QTreeWidgetItem * item, int column);
    // ...

private:
    QTreeWidget m_tree;
};

N.B.: I haven't tested the code above, it's just an example. There are plenty of tutorials around on the use of Qt signals and slots.

wakjah
  • 4,541
  • 1
  • 18
  • 23
0

Use QTreeWidget::itemChanged signal and signal/slot. Small working example:

QObject::connect(MyTree,&QTreeWidget::itemChanged,[=](QTreeWidgetItem * item,int column) {
    qDebug() << item->text(column);
});

I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.

Jablonski
  • 18,083
  • 2
  • 46
  • 47