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. [SOLVED]QTreeView setIndexWidget() for child nodes.
QtWS25 Last Chance

[SOLVED]QTreeView setIndexWidget() for child nodes.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 17.3k Views
  • 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.
  • S Offline
    S Offline
    Sam
    wrote on last edited by
    #1

    Hi,
    How can i add a QLabel/QPushButton/CustomWidget to a child node in QTreeView. I am able to get the same with QTreeWidget by using setItemWidget().

    I tried the following code

    @QStandardItem *children = new QStandardItem();
    QStandardItem *parent = model->item(r,c);
    parent->appendRow(children);

    QModelIndex index = model->indexFromItem(children);
    ui->treeView->setIndexWidget(index,myLabel);@

    Thanks.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Did you try do this?

      last time when i were needed something like this, i overloaded QItemDelegate::paint() method.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        Hi qxoz,

        How can i add a QLabel / CustomWidget in QItemDelegate::paint() function ? I can use a QPainter to drawRect , Text but how to display a QLabel or a custom widget ?

        Thanks.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          [quote author="Soumitra" date="1335956895"]Hi qxoz,

          How can i add a QLabel / CustomWidget in QItemDelegate::paint() function ? I can use a QPainter to drawRect , Text but how to display a QLabel or a custom widget ?

          Thanks.[/quote]
          You can't. That function is for painting, not for creating or setting widgets.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            [quote author="Andre" date="1335958493"]You can't. That function is for painting, not for creating or setting widgets.

            [/quote]Thanks Andre ,yes that function is only used for painting. I am trying to create a table with expanding and collapsing rows . So inorder to achieve that i am using a QTreeView and trying to set the setIndexWidget(). I am able to get some information from "this":http://qt-project.org/forums/viewthread/14917 and "this":http://qt-project.org/forums/viewthread/15909 but using QTreeWidget. I need to implement using QTreeView.

            This is the "image":http://imageshack.us/photo/my-images/692/capturenu.png/

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qxoz
              wrote on last edited by
              #6

              Look at:
              "link1":http://stackoverflow.com/questions/3656306/how-to-put-an-image-and-a-qprogressbar-inside-a-qtableview
              "link2":http://www.qtcentre.org/threads/31588-QCombobox-as-QTableView-delegate

              There are some information

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                Hi qxoz,

                Thanks for the links . I'll look into the code. BTW do you know how to create a QTableView with Expanding and Collapsing row. I am looking just for an approach to get this one. I tried this with QTreeWidget but need to implement it with QTreeView and set the tableView to the child nodes.

                Thanks for your time :)

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  Hi qxoz,

                  I have gone through the above links, but still i didnt get the solution about how to add custom widget to the child nodes of QTreeView. I changed my code as per what is specified on those two links.
                  I think its will be good if i can use setIndexWdget(). It will be really helpful someone can help me on how to setIndexWidget() for child nodes in QTreeView.

                  Thanls

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    stima_ua
                    wrote on last edited by
                    #9

                    @
                    QTreeWidgetItem *parent = new QTreeWidgetItem;
                    myThreeWidget->setItemWidget(parent, column, widget);
                    myThreeWidget->addTopLevelItem(parent);


                    QTreeWidgetItem *child = new QTreeWidgetItem(parent); //<<--Set parent
                    myThreeWidget->setItemWidget(child, column, widget);
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sam
                      wrote on last edited by
                      #10

                      [quote author="stima_ua" date="1336029386"]@
                      QTreeWidgetItem *parent = new QTreeWidgetItem;
                      myThreeWidget->setItemWidget(parent, column, widget);
                      myThreeWidget->addTopLevelItem(parent);


                      QTreeWidgetItem *child = new QTreeWidgetItem(parent); //<<--Set parent
                      myThreeWidget->setItemWidget(child, column, widget);
                      @[/quote]

                      Hi stima_ua,

                      Thanks for the code, but unfortunately i need to implement this using QTreeView setIndexWidget() function and not QTreeWidget setItemWidget() function. I have already implemented this using QTreeWidget.

                      Thanks for your time :)

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        stima_ua
                        wrote on last edited by
                        #11

                        @
                        #include <QtGui>
                        #include <QtCore>

                        int main(int argc, char *argv[])
                        {
                        QApplication a(argc, argv);
                        QTreeView view;
                        QStandardItemModel model;
                        const int WidgetRole = Qt::UserRole + 1;

                        view.setModel(&model);
                        
                        QLabel *parentWidget = new QLabel;
                        QStandardItem *parent = new QStandardItem;
                        
                        parentWidget->setText("parent");
                        parentWidget->setAutoFillBackground(true);
                        //For use ss you should reimplement paint;
                        //parentWidget->setStyleSheet("QLabel#parentWidget { background: red; }");
                        
                        model.appendRow(parent);
                        parent->setData(qVariantFromValue((void*) parentWidget), WidgetRole);
                        parent->setData(Qt::Checked, Qt::CheckStateRole);
                        view.setIndexWidget(parent->index(), parentWidget);
                        
                        QStandardItem *child = new QStandardItem;
                        QLabel *childWidget = new QLabel;
                        
                        parent->setChild(0, child);
                        
                        childWidget->setText("child");
                        childWidget->setAutoFillBackground(true);
                        
                        child->setData(qVariantFromValue((void*) childWidget), WidgetRole);
                        child->setData(Qt::Checked, Qt::CheckStateRole);
                        
                        view.setIndexWidget(child->index(), childWidget);
                        
                        view.show();
                        
                        return a.exec&#40;&#41;;
                        

                        }

                        @

                        You can. But better use treewidget.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sam
                          wrote on last edited by
                          #12

                          Hi stima_ua,

                          This is exactly what i was looking for. Thanks a lot :)

                          Regards
                          Soumitra.

                          Edit: Finally I got something that looks like this :-

                          !http://img138.imageshack.us/img138/583/captureecb.png(exapandCollapse)!

                          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