Results 1 to 10 of 10

Thread: Custom tree view with checkbox and icon - Plz help

  1. #1
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Custom tree view with checkbox and icon - Plz help

    Hi all,

    I'm new to Qt and really need help with subclassing the QAbstractItemModel to create a custom tree with:
    1) checkbox or radio button or nothing
    2) icon
    3) label1 - item name
    4) label2 - which is a count of label1's children

    See attached image. THANKS IN ADVANCE.


    Trish
    Attached Images Attached Images

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Custom tree view with checkbox and icon - Plz help

    Qt item views support checkable items with icons out of the box. All you need is to create a QItemDelegate subclass to render the default check box as a radio button instead. You can use QStyle for that.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb Re: Custom tree view with checkbox and icon - Plz help

    Thanks. I currently have the following code
    Qt Code:
    1. QStandardItem *parentItem = new QStandardItem(QIcon("C:\\dataset_small.png"), "Authors");
    2. model->appendRow(parentItem);
    3. QStandardItem *author1 = new QStandardItem(QIcon("C:\\dataset_small.png"), "Smith, V");
    4. author1->setCheckable(true);
    5. parentItem->appendRow(author1);
    6. parentItem->appendRow(new QStandardItem(QIcon("C:\\dataset_small.png"), "Campbell, C"));
    7. for (int i = 0; i < 3; ++i) {
    8. QStandardItem *item = new QStandardItem(QIcon("C:\\dataset_small.png"), QString("Book # %0").arg(i+1));
    9. item->setCheckable(true);
    10. author1->insertRow(i, item);
    11. }
    12. ui.treeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    producing the attached image. Remaining things are:
    1) I like the checkbox for the author node, but I'd like to have the radio button for the book items to force only one book can be selected at the time. Could you please elaborate on the QItemDelegate?
    2) I also want to have a count of children next to the labels meaning I want to have a "2" next to the "Authors" string (since there are 2 authors) and a "3" next to "Smith, V" because he has 3 books
    Attached Images Attached Images

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Custom tree view with checkbox and icon - Plz help

    Quote Originally Posted by ttvo View Post
    1) I like the checkbox for the author node, but I'd like to have the radio button for the book items to force only one book can be selected at the time. Could you please elaborate on the QItemDelegate?
    The exclusively checked items should be handled at model side, for example by reimplementing QStandardItem::setData() and handling Qt::CheckStateRole. The visual appearance can be handled by overriding QItemDelegate::drawCheck(). Construct a style option and call QStyle::drawPrimitive(QStyle::PE_IndicatorRadioBut ton).

    2) I also want to have a count of children next to the labels meaning I want to have a "2" next to the "Authors" string (since there are 2 authors) and a "3" next to "Smith, V" because he has 3 books
    You could reimplement QStandardItem::data() and append rowCount() to Qt::DisplayRole.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom tree view with checkbox and icon - Plz help

    Quote Originally Posted by jpn View Post
    The exclusively checked items should be handled at model side, for example by reimplementing QStandardItem::setData() and handling Qt::CheckStateRole. The visual appearance can be handled by overriding QItemDelegate::drawCheck(). Construct a style option and call QStyle::drawPrimitive(QStyle::PE_IndicatorRadioBut ton).
    ok, I add the following code for the radiobutton
    Qt Code:
    1. void RadioButtonDelegate::drawCheck(QPainter *painter,
    2. const QStyleOptionViewItem &option,
    3. const QRect &rect, Qt::CheckState state) const
    4. {
    5. // Q_D(const QItemDelegate);
    6. if (!rect.isValid())
    7. return;
    8.  
    9. QStyleOptionViewItem opt(option);
    10. opt.rect = rect;
    11. opt.state = opt.state & ~QStyle::State_HasFocus;
    12.  
    13. switch (state) {
    14. case Qt::Unchecked:
    15. opt.state |= QStyle::State_Off;
    16. break;
    17. case Qt::PartiallyChecked:
    18. opt.state |= QStyle::State_NoChange;
    19. break;
    20. case Qt::Checked:
    21. opt.state |= QStyle::State_On;
    22. break;
    23. }
    24.  
    25. // const QWidget *widget = d->widget(option);
    26. QStyle *style = QApplication::style();
    27. style->drawPrimitive(QStyle::PE_IndicatorRadioButton, &opt, painter);
    28. }
    To copy to clipboard, switch view to plain text mode 
    Note that I had to comment out the Q_D(const QItemDelegate) here in order for my code to compile
    1) what is the equivalent code for "Q_D(const QItemDelegate);"? I'd like to understand this since the Q_D appears in QStandardItem::setdata() and data().
    2) My tree now only has radiobuttons. How can I customize it such that the author node has checkboxes and only the book node has radiobuttons?
    Many thx in advance.
    You could reimplement QStandardItem::data() and append rowCount() to Qt:isplayRole.
    I ran into the Q_D(...) issue as described above so I didn't get far.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Custom tree view with checkbox and icon - Plz help

    Quote Originally Posted by ttvo View Post
    Note that I had to comment out the Q_D(const QItemDelegate) here in order for my code to compile
    1) what is the equivalent code for "Q_D(const QItemDelegate);"? I'd like to understand this since the Q_D appears in QStandardItem::setdata() and data().
    Q_D is a macro that declares a d-pointer to the private implementation (ie. QItemDelegate -> QItemDelegatePrivate). You cannot use private parts of Qt, you have to get along without them...

    2) My tree now only has radiobuttons. How can I customize it such that the author node has checkboxes and only the book node has radiobuttons?
    You have to identify it somehow. You can for example store the information to a custom role, or you can check the parent(s) of the model index to see where it is located in the tree.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    ttvo (25th February 2009)

  8. #7
    Join Date
    Oct 2009
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom tree view with checkbox and icon - Plz help

    Hi guys,

    I'm currently solving almost the same problem, especially the following one:

    Quote Originally Posted by jpn View Post
    The exclusively checked items should be handled at model side, for example by reimplementing QStandardItem::setData()
    I wonder how did you implement the QStandardItem::setData method ? It looks that the only way to do that is
    1. Go through all items in the model and un-check them
    2. Run the default implementation for the being checked item so that the new state is properly processed


    So something like this:

    Qt Code:
    1. void uncheckBranchItems(QStandardItem * parent_item)
    2. {
    3. for (int i = 0; i < parent_item->rowCount(); ++i)
    4. {
    5. QStandardItem * child_item = parent_item->child(i);
    6. if (Qt::Checked == child_item->checkState())
    7. {
    8. child_item->setCheckState(Qt::Unchecked);
    9. }
    10.  
    11. uncheckBranchItems(child_item);
    12. }
    13. }
    14.  
    15. void RadioButtonItem::setData(const QVariant & value, int role)
    16. {
    17. if (Qt::CheckStateRole == role)
    18. {
    19. if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
    20. {
    21. uncheckBranchItems(model()->invisibleRootItem());
    22. }
    23. }
    24.  
    25. QStandardItem::setData(value, role);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Now imagine that I have another object which subscribes to the QTreeView::clicked() signal, check the item check state and do some custom logic then. I need somehow emulate the signal for the items being un-checked during call to uncheckBranchItems(). The signal should be emitted from the model, not from the item. Because possible subscribers to the signal do not know anything about the items but about model only. How would you do that ?

    I'm actually curious what was your implementation of QStandardItem::setData method and whether you implemented some signal notifications on current checked item changes. Probably my solution is bad and you could advise something better where the required signal notifications are easy to do?

  9. #8
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Custom tree view with checkbox and icon - Plz help

    Hi all,

    I'm doing the same as you. How did you put the radioButton? Or simply you implement the above algorithm?

    I see that you subclassed the RadioButtonItem, could be?

    So, can you post the final code?

    Sorry for my poor English.

    Many thanks!!!

  10. #9
    Join Date
    Apr 2011
    Location
    Palma de Mallorca, Islas Baleares, Spain
    Posts
    24
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Smile Re: Custom tree view with checkbox and icon - Plz help

    Hi,

    I solved almost this problem.
    The code is:

    In MainWindow I have:
    Qt Code:
    1. void MainWindow::startTree(){ QStandardItemModel *model = new QStandardItemModel();
    2. for( int r=0; r<5; r++ ){
    3. SubQStandardItem *item = new SubQStandardItem( QString("Row:%1").arg(r) );
    4. item->setEditable(false);
    5. for( int i=0; i<3; i++ ){
    6. SubQStandardItem *subItem = new SubQStandardItem( QString("Item %1").arg(i) );
    7. subItem->setCheckable(true);
    8. subItem->setEditable(false);
    9. item->appendRow(subItem);
    10. }
    11. model->setItem(r, item);
    12. }
    13. view = new QTreeView(centralWidget);
    14. view->setModel(model);
    15. view->show();
    16. }
    To copy to clipboard, switch view to plain text mode 
    SubQStandardItem.h:
    Qt Code:
    1. /* * SubQStandardItem.h
    2.  *
    3.  * Created on: 23/09/2011
    4.  * Author: Sergio Madrazo Giménez
    5.  */
    6.  
    7.  
    8. #ifndef SUBQSTANDARDITEM_H_
    9. #define SUBQSTANDARDITEM_H_
    10. #include <QStandardItem>
    11.  
    12.  
    13. class SubQStandardItem : public QStandardItem
    14. {
    15.  
    16.  
    17. public:
    18. SubQStandardItem(QString);
    19. virtual ~SubQStandardItem();
    20.  
    21.  
    22. private:
    23. void setData(const QVariant & value, int role);
    24. void uncheckBranchItems(QStandardItem * parent_item);
    25. };
    26.  
    27.  
    28. #endif /* SUBQSTANDARDITEM_H_ */
    To copy to clipboard, switch view to plain text mode 
    SubQStandardItem.cpp:
    Qt Code:
    1. /* * SubQStandardItem.cpp
    2.  *
    3.  * Created on: 23/09/2011
    4.  * Author: smadrazo
    5.  */
    6.  
    7.  
    8. #include "SubQStandardItem.h"
    9.  
    10.  
    11. SubQStandardItem::SubQStandardItem(QString qs)
    12.  
    13.  
    14. {
    15. // TODO Auto-generated constructor stub
    16. }
    17.  
    18.  
    19. SubQStandardItem::~SubQStandardItem() {
    20. // TODO Auto-generated destructor stub
    21. }
    22.  
    23.  
    24. void SubQStandardItem::setData(const QVariant & value, int role)
    25. {
    26. QStandardItem::setData(value, role);
    27. if (Qt::CheckStateRole == role){
    28. if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
    29. uncheckBranchItems(model()->invisibleRootItem());
    30. }
    31. QStandardItem::setData(value, role);
    32. }
    33.  
    34.  
    35. void SubQStandardItem::uncheckBranchItems(QStandardItem * parent_item){
    36. int count=0;
    37. for (int i = 0; i < parent_item->rowCount(); ++i)
    38. {
    39. QStandardItem * child_item = parent_item->child(i);
    40. if (Qt::Checked == child_item->checkState())
    41. count++;
    42. uncheckBranchItems(child_item);
    43. }
    44. if(count>1){
    45. for (int i = 0; i < parent_item->rowCount(); ++i){
    46. QStandardItem * child_item = parent_item->child(i);
    47. child_item->setCheckState(Qt::Unchecked);
    48. }
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 

    This code checks only one item of the row.

    I hope it help you!

  11. #10
    Join Date
    Jul 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Custom tree view with checkbox and icon - Plz help

    Quote Originally Posted by ttvo View Post
    Thanks. I currently have the following code
    Qt Code:
    1. QStandardItem *parentItem = new QStandardItem(QIcon("C:\\dataset_small.png"), "Authors");
    2. model->appendRow(parentItem);
    3. QStandardItem *author1 = new QStandardItem(QIcon("C:\\dataset_small.png"), "Smith, V");
    4. author1->setCheckable(true);
    5. parentItem->appendRow(author1);
    6. parentItem->appendRow(new QStandardItem(QIcon("C:\\dataset_small.png"), "Campbell, C"));
    7. for (int i = 0; i < 3; ++i) {
    8. QStandardItem *item = new QStandardItem(QIcon("C:\\dataset_small.png"), QString("Book # %0").arg(i+1));
    9. item->setCheckable(true);
    10. author1->insertRow(i, item);
    11. }
    12. ui.treeView->setModel(model);
    To copy to clipboard, switch view to plain text mode 
    producing the attached image. Remaining things are:
    1) I like the checkbox for the author node, but I'd like to have the radio button for the book items to force only one book can be selected at the time. Could you please elaborate on the QItemDelegate?
    2) I also want to have a count of children next to the labels meaning I want to have a "2" next to the "Authors" string (since there are 2 authors) and a "3" next to "Smith, V" because he has 3 books


    hey ...What About TreeView Qml Type

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.