[SOLVED]QTreeView setIndexWidget() for child nodes.



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



  • Did you try do this?

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



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



  • [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/





  • 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 :)



  • 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



  • @
    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 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 :)



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



  • 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)!


Log in to reply
 

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