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. how can i get the QTreeWidgetItem from its itemWidget?
Forum Updated to NodeBB v4.3 + New Features

how can i get the QTreeWidgetItem from its itemWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 3.8k Views 1 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          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?

          O 1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            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?

            O Offline
            O Offline
            opengpu2
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #6

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

              O 1 Reply Last reply
              0
              • Paul ColbyP Paul Colby

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

                O Offline
                O Offline
                opengpu2
                wrote on last edited by opengpu2
                #7

                @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

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  opengpu2
                  wrote on last edited by
                  #8

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

                  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