Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeWidget: get QTreeWidgetItem* by internal widget



  • Hi
    How to get QTreeWidgetItem after, for example, QPushButton clicked()?
    I receive QWidget by sender(), but I need to rebuild part of QTreeWidget after click, so I need to control QTreeWidgetItem.
    I trying to do this without QTreeWidgetItem pointers saving.


  • Lifetime Qt Champion

    Hi,

    After a click on what exactly ?
    What is that you want to retrieve from the QTreeWidget ?
    All the items are accessible but you need to give a bit more details about what you want to do.



  • @SGaist hi!
    QTreeWidgetItem contains QPushButton.
    This QTreeWidgetItem contains also child QTreeWidgetItems...
    After QPushButton click I need to remove all children and create new ones...



  • @sitesv said in QTreeWidget: get QTreeWidgetItem* by internal widget:

    QTreeWidgetItem contains QPushButton.

    In what sense "contains"? How have you added a QPushButton to a QTreeWidgetItem, how are they linked? QTreeWidget::setItemWidget()?? In which case I think you have to walk the QTreeWidget calling QTreeWidget::itemWidget() to locate it.



  • @JonB said in QTreeWidget: get QTreeWidgetItem* by internal widget:

    In what sense "contains"? How have you added a QPushButton to a QTreeWidgetItem, how are they linked? QTreeWidget::setItemWidget()??

    Hi!
    Is there any other variant of adding? I know only one: setItemWidget().



  • @sitesv
    No, but you didn't mention setItemWidget() anywhere, so I asked. Given that, as I said as far as I know you will need to iterate the tree's items' widgets (via QTreeWidget::itemWidget()) to find the widget (QPushButton) you are interested in.



  • @JonB said in QTreeWidget: get QTreeWidgetItem* by internal widget:

    @sitesv
    No, but you didn't mention setItemWidget() anywhere, so I asked. Given that, as I said as far as I know you will need to iterate the tree's items' widgets (via QTreeWidget::itemWidget()) to find the widget (QPushButton) you are interested in.

    I have one idea: if I will get the selected item when pushbutton signal emits? Is this approach correct?
    Of course - I will set Treewidget selection mode as a single selection mode....



  • Disclaimer

    setItemWidget() is 99.9% of the time the wrong solution to a problem, a custom delegate is probably what you want and should use


    Practical solution: before adding the button to the tree call button->setproperty("IndexOfButton",QPersistentModelIndex(idx)) where idx is the index of the cell where you are inserting the button. Then you can just read the property to get the index back.

    I can't stress enough how a custom delegate would be the correct way to do this


    P.S.
    From Qt 5.2 onward sender() is also bad design. You should pass it as an argument to the slot:

    • void onClicked(); becomes void onClicked(QPushButton* sender);
    • connect(button,&QPushButton::clicked,this,&MyClass::onClicked); becomes connect(button,&QPushButton::clicked,this,std::bind(&MyClass::onClicked,this,button));


  • This post is deleted!


  • @VRonin
    Hi
    About 99.9% of cases: I tried to make Tree via TreeView, but I was unable to achieve a persistent display of widgets because of using delegates... So I made a conclusion of using TreeWidget... So, I could save a TreeWidgetItem pointer as a button property...
    But what about using 'selectedItems' method? I think this is a solution.

    Thank you about 'sender' design.



  • @sitesv said in QTreeWidget: get QTreeWidgetItem* by internal widget:

    I was unable to achieve a persistent display of widgets because of using delegates

    You just need to reimplement paint() and fill a QStyleOptionButton to to show the button



  • @VRonin said in QTreeWidget: get QTreeWidgetItem* by internal widget:

    You just need to reimplement paint() and fill a QStyleOptionButton to to show the button

    Hi!
    Is it possible to show ComboBox and CheckBox as you wrote?


Log in to reply