Results 1 to 9 of 9

Thread: Disable Single Checkbox in QTreeWidgetItem

  1. #1
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Disable Single Checkbox in QTreeWidgetItem

    Hello,

    Any one know a way to disable a single chekbox in Qtreewidget.
    i Have list of QTreeWidgetItem*, all are in Qt::Unchecked state;
    i need to disbale few chekbox's.
    When i use QTreeWidgetItem::SetFlags(Qt::ItemIsEditable), checkbox's in complete row change to disable mode.
    How can i disable only one chekbox in QTreeWidgetItem.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    When i use QTreeWidgetItem::SetFlags(Qt::ItemIsEditable), checkbox's in complete row change to disable mode.
    Please post relevant code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Exclamation Re: Disable Single Checkbox in QTreeWidgetItem

    --------------------------------------------H---------------------------------------------------
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. void initTree();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21.  
    22. private slots:
    23.  
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    ------------------------------------CPP-----------------------------------------------------
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QTreeWidget>
    4. #include <QTreeWidgetItem>
    5. #include "MyStyle.h"
    6. #include <QMultiMap>
    7.  
    8. QTreeWidget* pTree;
    9. QList<QTreeWidgetItem*> lstWidgets;
    10. QList<QTreeWidgetItem*> lstSubItems;
    11.  
    12.  
    13. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    14. {
    15. ui->setupUi(this);
    16. pTree = NULL;
    17. initTree();
    18. QSize size(540,870);
    19. this->resize(size);
    20. this->setMinimumSize(size);
    21. this->setMaximumSize(size);
    22. ui->gridLayout->addWidget(pTree);
    23. }
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. }
    29.  
    30. void MainWindow::initTree()
    31. {
    32. pTree = new QTreeWidget(this);
    33. pTree->setGeometry(10,10,520,850);
    34. pTree->setMaximumSize(520,850);
    35. pTree->show();
    36.  
    37. QStringList lstheadrs;
    38. lstheadrs.append(" \n \n \n \n \n \n ");
    39. lstheadrs.append("1");
    40. lstheadrs.append("2");
    41. lstheadrs.append("3");
    42. lstheadrs.append("4");
    43. lstheadrs.append("5");
    44. lstheadrs.append("6");
    45. lstheadrs.append("7");
    46. lstheadrs.append("8");
    47. lstheadrs.append("9");
    48. lstheadrs.append("10");
    49. lstheadrs.append("11");
    50. lstheadrs.append("12");
    51. pTree->setHeaderLabels(lstheadrs);
    52. //------SET COLUMN WIDTH---------------
    53. pTree->setColumnWidth(0,190);
    54. for(int i = 1; i<=12; i++)
    55. {
    56. pTree->setColumnWidth(i,25);
    57. }
    58. MyStyle *headerStyl = new MyStyle(this->style());
    59. pTree->setStyle(headerStyl);
    60.  
    61. //==========================================================================================================//
    62. QTreeWidgetItem *Node1 = new QTreeWidgetItem(pTree, QStringList(QObject::tr("1")));
    63. Node1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("1.1"))));
    64. Node1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("1.2"))));
    65. Node1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("1.3"))));
    66. pTree->expandItem(Node1);
    67. for(int iChild=0; iChild<Node1->childCount() ; iChild++)
    68. for(int i=1; i<lstheadrs.length(); i++)
    69. Node1->child(iChild)->setCheckState(i,Qt::Unchecked);
    70.  
    71. lstWidgets.append(Node1);
    72. //==========================================================================================================//
    73. QTreeWidgetItem *Node2 = new QTreeWidgetItem(pTree, QStringList(QObject::tr("2")));
    74. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.1"))));
    75. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.2"))));
    76. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.3"))));
    77. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.4"))));
    78. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.5"))));
    79. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.6"))));
    80. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.7"))));
    81. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.8"))));
    82. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.9"))));
    83. Node2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("2.10"))));
    84. pTree->expandItem(Node2);
    85. for(int iChild=0; iChild<Node2->childCount() ; iChild++)
    86. for(int i=1; i<lstheadrs.length(); i++)
    87. Node2->child(iChild)->setCheckState(i,Qt::Unchecked);
    88.  
    89. lstWidgets.append(Node2);
    90. //==========================================================================================================//
    91. QTreeWidgetItem *Node3 = new QTreeWidgetItem(pTree,QStringList(QObject::tr("3")));
    92.  
    93. QTreeWidgetItem *node3sub1 = new QTreeWidgetItem(Node3, QStringList(QObject::tr("3.1")));
    94. node3sub1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.1.1"))));
    95. node3sub1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.1.2"))));
    96. node3sub1->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.1.3"))));
    97. Node3->addChild(node3sub1);
    98. for(int iChild=0; iChild<node3sub1->childCount(); iChild++)
    99. for(int i=1; i<lstheadrs.length(); i++)
    100. node3sub1->child(iChild)->setCheckState(i,Qt::Unchecked);
    101.  
    102. QTreeWidgetItem *node3sub2 = new QTreeWidgetItem(Node3, QStringList(QObject::tr("3.2")));
    103. node3sub2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.2.1"))));
    104. node3sub2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.2.2"))));
    105. node3sub2->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.2.3"))));
    106. Node3->addChild(node3sub2);
    107. for(int iChild=0; iChild<node3sub2->childCount(); ++iChild)
    108. for(int i=1; i<lstheadrs.length(); i++)
    109. node3sub2->child(iChild)->setCheckState(i,Qt::Unchecked);
    110.  
    111. QTreeWidgetItem *node3sub3 = new QTreeWidgetItem(Node3, QStringList(QObject::tr("3.3")));
    112. node3sub3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.3.1"))));
    113. node3sub3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.3.2"))));
    114. node3sub3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.3.3"))));
    115. Node3->addChild(node3sub3);
    116. for(int iChild=0; iChild<node3sub3->childCount(); iChild++)
    117. for(int i=1; i<lstheadrs.length(); i++)
    118. node3sub3->child(iChild)->setCheckState(i,Qt::Unchecked);
    119.  
    120.  
    121. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.4"))));
    122. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.5"))));
    123. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.6"))));
    124. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.7"))));
    125. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.8"))));
    126. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.9"))));
    127. Node3->addChild(new QTreeWidgetItem(QStringList(QObject::tr("3.10"))));
    128.  
    129. pTree->expandItem(Node3);
    130. for(int iChild=0; iChild<Node3->childCount() ; iChild++)
    131. for(int i=1; i<lstheadrs.length(); i++)
    132. Node3->child(iChild)->setCheckState(i,Qt::Unchecked);
    133.  
    134. lstWidgets.append(Node3);
    135. lstWidgets.append(node3sub1);
    136. lstWidgets.append(node3sub2);
    137. lstWidgets.append(node3sub3);
    138. //==========================================================================================================//
    139. QTreeWidgetItem *node4 = new QTreeWidgetItem(pTree, QStringList(QObject::tr("4")));
    140. node4->addChild(new QTreeWidgetItem(QStringList(QObject::tr("4.1"))));
    141. node4->addChild(new QTreeWidgetItem(QStringList(QObject::tr("4.2"))));
    142. pTree->expandItem(node4);
    143. for(int iChild=0; iChild<node4->childCount() ; iChild++)
    144. for(int i=1; i<lstheadrs.length(); i++)
    145. node4->child(iChild)->setCheckState(i,Qt::Unchecked);
    146.  
    147. lstWidgets.append(node4);
    148. //==========================================================================================================//
    149. QTreeWidgetItem *node5 = new QTreeWidgetItem(pTree, QStringList(QObject::tr("5")));
    150. node5->addChild(new QTreeWidgetItem(QStringList(QObject::tr("5.1"))));
    151. node5->addChild(new QTreeWidgetItem(QStringList(QObject::tr("5.2"))));
    152. node5->addChild(new QTreeWidgetItem(QStringList(QObject::tr("5.3"))));
    153. node5->addChild(new QTreeWidgetItem(QStringList(QObject::tr("5.4"))));
    154. pTree->expandItem(node5);
    155. for(int iChild=0; iChild<node5->childCount() ; iChild++)
    156. for(int i=1; i<lstheadrs.length(); i++)
    157. node5->child(iChild)->setCheckState(i,Qt::Unchecked);
    158.  
    159. lstWidgets.append(node5);
    160. }
    To copy to clipboard, switch view to plain text mode 


    This Code will generate a treeviwe, i need to disable only few checkboxes.
    for example chekbox at index (1,3) (1,4),(1,5) (2,2),(2,5),(2,6) like vis.

    But time to time the disable checkbox index may change,
    disabled indexed previously will be active at the next viwe.
    how can i contol this.

    Note : lstWidgets has all the row items...

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    When i use QTreeWidgetItem::SetFlags(Qt::ItemIsEditable), checkbox's in complete row change to disable mode.
    Where in the code you posted you call setFlags()?

    This Code will generate a treeviwe, i need to disable only few checkboxes.
    for example chekbox at index (1,3) (1,4),(1,5) (2,2),(2,5),(2,6) like vis.
    So I take it that you know how to do this part?

    But time to time the disable checkbox index may change,
    disabled indexed previously will be active at the next viwe.
    how can i contol this.
    not sure what you mean, can you give a use case example?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    Thanks for replying.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    2. {
    3. ui->setupUi(this);
    4. pTree = NULL;
    5. initTree();
    6. QSize size(540,870);
    7. this->resize(size);
    8. this->setMinimumSize(size);
    9. this->setMaximumSize(size);
    10. ui->gridLayout->addWidget(pTree);
    11.  
    12. // Add this 3 lines to the code
    13. ////////////////////////////////////////////////////////////////////////////
    14. Line 1)
    15. lstWidgets.at(2)->child(5)->setCheckState(4,Qt::Checked);
    16. Line 2)
    17. lstWidgets.at(2)->child(4)->setFlags(Qt::ItemIsEditable);
    18. Line 3)
    19. // lstSubItems.at(5)->setFlags(Qt::ItemIsEditable);
    20.  
    21. //////////////////////////////////////////////////////////////////////////////
    22. }
    To copy to clipboard, switch view to plain text mode 

    1)
    after adding the (Line1) and running the program u will have Qtree with only one item in Qt::checked state.
    what i wont to know is how to disable this checkbox so users cant change this.

    when i try to use setFlags....
    2)
    after adding the (Line2) you will find that the full line at given index changes to disable state
    3)
    Line 3 Gives a runtime error

  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Disable Single Checkbox in QTreeWidgetItem


  7. #7
    Join Date
    May 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    Quote Originally Posted by MarekR22 View Post
    If I understood the OP correctly, deepal_de only wants to disable the checkbox, not the whole item row.

  8. The following user says thank you to ChristianSeverin for this useful post:

    deepal_de (25th May 2011)

  9. #8
    Join Date
    Jan 2011
    Location
    Sri Lanaka
    Posts
    64
    Thanks
    39
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    Yes thats correct

  10. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Disable Single Checkbox in QTreeWidgetItem

    Ok I should also give this link: QAbstractItemModel::flags. Check QStandardItem::setFlags if you use standard model (QStandardItemModel).
    Just in case take a look also here:
    QAbstractItemModel::data Qt::ItemDataRole-enum see Qt::CheckStateRole
    Last edited by MarekR22; 25th May 2011 at 09:33.

  11. The following user says thank you to MarekR22 for this useful post:

    deepal_de (26th May 2011)

Similar Threads

  1. checkbox
    By fluefiske in forum Newbie
    Replies: 3
    Last Post: 5th October 2010, 22:41
  2. Replies: 5
    Last Post: 23rd September 2010, 13:58
  3. QTreeWidgetItem and checkbox detection
    By merlvingian in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2008, 05:16
  4. QTreeWidget Single Column Sort Disable
    By craigjameshamilton in forum Newbie
    Replies: 1
    Last Post: 21st April 2008, 08:08
  5. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21:59

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.