Nominate our 2022 Qt Champions!

how can i get the QTreeWidgetItem from its itemWidget?

  • how can i get the QTreeWidgetItem from its itemWidget?
    i use setItemWidget to add a QPushButton on a column of QTreeWidgetItem, and in the QPushButton's clicked slot i want to gef the related QTreeWidgetItem.
    is there any good way to do this?
    thank you!

  • The documentation for QTreeViewWidget::setItemWidget says:

    Note: The tree takes ownership of the widget.

    So, can probably do something like:

    QTreeViewWidget * const treeViewWidget = qobject_cast<QTreeViewWidget *>(parent());


  • thank you , but i want the related QTreeWidgetItem not QTreeWidget...

  • Oops. Sorry... didn't read carefully enough :)

    Which class is implementing the slot that the QPushButton::clicked signal is connected to? A custom QTreeWidget or QTreeWidgetItem?

    How are you adding the QPushButton instances to the QTreeWidget?

  • @Paul-Colby use setItemWidget to add a QPushButton.
    i want to get the related QTreeWidgetItem in the QPushButton's clicked slot, because there are some custom data stored in the other column of the QTreeWidgetItem which using setData().

  • @opengpu2 said:

    i want to get the related QTreeWidgetItem in the QPushButton's clicked slot

    QPushButton doesn't have a clicked slot - it has a clicked signal. So you have to connect that signal to a slot somewhere. Depending on where you want to encapsulate the logic, that slot could be on a custom QTreeWidget, QTreeWidgetItem, or QPushButton.

    For example, using a slot in a custom QTreeWidget (pseudo code only):

    // in header: QMap<QPushButton *, QTreeWidgetItem *> buttonItemMap;
    void MyTreeWidget::MyTreeWidget() : QTreeWidget() {
        QTreeWidgetItem * item = new QTreeWidgetItem(this);
        QPushButton * button = new QPushButton(this);
        buttonItemMap.insert(button, item);
        connect(button, SIGNAL(clicked()), myTreeWidget, SLOT(buttonClicked));
        myTreeWidget->setItemWidget(item , 0, button);
    void MyTreeWidget::buttonClicked() {
        QPushButton * const button = qobject_cast<QPushButton *>(sender());
        QTreeWidgetItem * const item =;
        // do what you want with 'item'.

    Using a slot in a custom QTreeWidgetItem (pseudo code only):

    void MyClass::wherever() {
        MyTreeWidgetItem * myTreeWidgetItem = new QTreeWidgetItem;
        QPushButton * button = new QPushButton(this);
        connect(button, SIGNAL(clicked()), myTreeWidgetItem, SLOT(buttonClicked));
        treeWidget->setItemWidget(myTreeWidgetItem , 0, button);
    MyTreeWidgetItem::buttonClicked() {
        // do whatever you want with 'this'.

    Using a slot in a custom QPushButton (pseudo code only):

    void MyClass::wherever() {
        QTreeWidgetItem * item = new QTreeWidgetItem;
        MyPushButton * myButton = new MyPushButton(this);
        connect(myButton, SIGNAL(clicked()), myTreeWidgetItem, SLOT(onClicked));
        treeWidget->setItemWidget(item , 0, button);
    void MyPushButton::setItem(QTreeWidgetItem * item) {
        this->item = item;
    // Note, this is a new onClicked function, not the base class' clicked signal.
    // Alternatively, you could probably override QPushButton::onMouseUp?
    void MyPushButton::onClicked() {
        // do whatever you want with this->item.

    Again, it depends on where it makes most sense to have your logic that uses the item, but the custom QTreeWidgetItem is likely to be the most logical otherwise a custom QTreeWidget. The custom QPushButton is likely to be wrong approach I think.

    Also, note in the QTreeWidget::setItemWidget documentation says:

    This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or implement a custom editor widget, use QTreeView and subclass QItemDelegate instead.

    I suspect that a QPushButton would not be considered static content, and you might need to be using a delegate instead.


  • @Paul-Colby thank you very much!
    actually i want to add a button like this pic below, the 2nd and 3th column, which is checkable.

  • thank you
    i did use delegate to edit a QTreeWidgetItem.
    howerver, i donnot know how to use delegate to add 2 buttons before the original QTreeWidgetItem and its icon...
    and i want to react the signals of the 2 buttons...

Log in to reply