Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeWidget: get QTreeWidgetItem* by internal widget
Forum Updated to NodeBB v4.3 + New Features

QTreeWidget: get QTreeWidgetItem* by internal widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by SGaist
    #2

    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.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    sitesvS 1 Reply Last reply
    1
    • SGaistS SGaist

      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.

      sitesvS Offline
      sitesvS Offline
      sitesv
      wrote on last edited by
      #3

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

      JonBJ 1 Reply Last reply
      0
      • sitesvS sitesv

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

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @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.

        sitesvS 1 Reply Last reply
        1
        • JonBJ JonB

          @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.

          sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by
          #5

          @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().

          JonBJ 1 Reply Last reply
          0
          • sitesvS sitesv

            @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().

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #6

            @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.

            sitesvS 1 Reply Last reply
            0
            • JonBJ JonB

              @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.

              sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by sitesv
              #7

              @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....

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #8

                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));

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                JonBJ sitesvS 2 Replies Last reply
                3
                • VRoninV VRonin

                  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));
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9
                  This post is deleted!
                  1 Reply Last reply
                  1
                  • VRoninV VRonin

                    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));
                    sitesvS Offline
                    sitesvS Offline
                    sitesv
                    wrote on last edited by sitesv
                    #10

                    @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.

                    VRoninV 1 Reply Last reply
                    0
                    • sitesvS sitesv

                      @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.

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

                      @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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      sitesvS 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        @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

                        sitesvS Offline
                        sitesvS Offline
                        sitesv
                        wrote on last edited by
                        #12

                        @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?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved