Results 1 to 5 of 5

Thread: how to add check box inside qtree widget?

  1. #1
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default how to add check box inside qtree widget?

    Hi all,
    In my project i'm using a tree widget.
    Which i created using designer...
    Now i wanted to add checkbox at each child..
    How can i do that?
    My code to create the tree widget as shown below
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include <QtCore>
    4. #include <QtGui>
    5.  
    6. Dialog::Dialog(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::Dialog)
    9. {
    10.  
    11. ui->setupUi(this);
    12.  
    13. ui->treeWidget->setColumnCount(2);
    14. ui->treeWidget->setHeaderLabels(QStringList()<< "one"<<"two");
    15. AddRoot("1 first","tree");
    16. AddRoot("2 second","person");
    17. AddRoot("3 third","man");
    18. AddRoot("4 fourth","and last");
    19.  
    20. }
    21.  
    22. Dialog::~Dialog()
    23. {
    24. delete ui;
    25. }
    26. void Dialog::AddRoot(QString name,QString Description)
    27. {
    28. QTreeWidgetItem *itm =new QTreeWidgetItem(ui->treeWidget);
    29. itm->setText(0,name);
    30. itm->setText(1,Description);
    31.  
    32.  
    33. AddChild(itm,"one","1111");
    34. AddChild(itm,"two","2222");
    35. }
    36.  
    37. void Dialog::AddChild(QTreeWidgetItem *parent,QString name, QString Description)
    38. {
    39. itm->setText(0,name);
    40. itm->setText(1,Description);
    41. parent->addChild(itm);
    42. }
    43.  
    44. void Dialog::on_pushButton_clicked()
    45. {
    46. ui->treeWidget->currentItem()->setBackgroundColor(0,Qt::red);
    47.  
    48. ui->treeWidget->currentItem()->setBackgroundColor(1,Qt::blue);
    49. }
    To copy to clipboard, switch view to plain text mode 



    here i need check box to select - "one" ,"111"
    and "two","2222222"...
    please tell me how can i do that?

  2. #2
    Join Date
    Mar 2011
    Location
    Russia, Lipetsk
    Posts
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to add check box inside qtree widget?

    Hello.
    In QTableWidget I used
    setFlags ( Qt::ItemFlags flags )
    and add flags Qt::ItemIsUserCheckable.
    Try in your case.
    The QTreeWidget has the same features.

  3. #3
    Join Date
    Oct 2011
    Posts
    160
    Thanks
    31
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to add check box inside qtree widget?

    ok thank u....

  4. #4
    Join Date
    May 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to add check box inside qtree widget?

    @kunashir

    Can you mention the code excerpt which you used for ItemFlags usage?
    Thanks in advance!!

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to add check box inside qtree widget?

    You already have the name of the function, QTreeWidgetItem::setFlags(), its documentation, and the Qt::ItemFlag documentation it links to. You even have the the name of the specific flag, Qt::ItemIsUserCheckable. Is it really such a difficult thing to work out?
    Qt Code:
    1. // OR QTableWidgetItem *item = new QTableWidgetItem();
    2. // OR QStandardItem *item = new QStandardItem();
    3. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 14th December 2011, 12:51
  2. Qt Designer Size of table widget inside Tab Widget is not flexible.
    By akash in forum Qt Tools
    Replies: 2
    Last Post: 14th September 2011, 12:45
  3. Replies: 8
    Last Post: 28th June 2011, 15:57
  4. Qtree in C++
    By rrrrcem in forum General Programming
    Replies: 3
    Last Post: 12th July 2008, 23:39
  5. Check a point inside or outside a road?
    By kstking in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2007, 19:48

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.