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());
    Q_CHECK_PTR(treeViewWidget);
    

    Cheers.



  • 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());
        QT_CHECK_PTR(button)
        QTreeWidgetItem * const item = map.at(button);
        QT_CHECK_PTR(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);
        myButton->setItem(item);
        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.

    Cheers.



  • @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.
    http://help.autodesk.com/cloudhelp/2015/ENU/3DSMax/images/GUID-251FA108-D677-470F-85AB-F1CF690EC4C0.png



  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.