QListWidget添加QCheckBox控件

本文实现的是在QListWidget添加QCheckBox控件功能。

头文件:

 
  
  1. icdisplaymodel.h
  2. #ifndef ICDISPLAYMODEL_H
  3. #define ICDISPLAYMODEL_H
  4. #include <QWidget>
  5. #include "ui_icdisplaymodel.h"
  6. //#include "ptcData.h"
  7. #include <QListWidget>
  8. #include
  9. using namespace std;
  10. /** \file icdisplaymodel.h
  11. * \brief 显示模式.
  12. *
  13. * 控制点类的显示.\n
  14. *
  15. * \author
  16. * \version 0.1
  17. * \date 2015.1.12
  18. */
  19. class icDisplayModel : public QWidget
  20. {
  21. Q_OBJECT
  22. public :
  23. icDisplayModel (QWidget * parent = 0);
  24. ~ icDisplayModel ();
  25. bool start ();
  26. void initListWidget ( QString description );
  27. private :
  28. Ui ::icDisplayModel ui;
  29. private slots :
  30. void apply ();
  31. void cancel ();
  32. void selcetAll ();
  33. void selectNone ();
  34. private :
  35. //插入listwidget
  36. bool insertListWidget ( QString description );
  37. //获取qcheckbox选择状态
  38. void getCheckedState ();
  39. };
  40. #endif // ICDISPLAYMODEL_H

CPP文件

 
  
  1. #include "icdisplaymodel.h"
  2. #include <QCheckBox>
  3. icDisplayModel ::icDisplayModel ( QWidget * parent )
  4. : QWidget (parent )
  5. {
  6. ui .setupUi ( this);
  7. connect (ui . applyButton, SIGNAL (clicked ()), this, SLOT (apply ()));
  8. connect (ui . cancelButton, SIGNAL (clicked ()), this, SLOT (cancel ()));
  9. connect (ui . selectAllButton, SIGNAL (clicked ()), this, SLOT (selcetAll ()));
  10. connect (ui . selectNoneButton, SIGNAL (clicked ()), this, SLOT (selectNone ()));
  11. }
  12. icDisplayModel ::~icDisplayModel ()
  13. {
  14. }
  15. void icDisplayModel :: apply()
  16. {
  17. getCheckedState ();
  18. hide ();
  19. }
  20. void icDisplayModel :: cancel()
  21. {
  22. hide ();
  23. }
  24. //全选
  25. void icDisplayModel :: selcetAll()
  26. {
  27. int count = ui. listWidget ->count ();
  28. for (int i=0; i <count ; i++)
  29. {
  30. QListWidgetItem * item = ui. listWidget ->item ( i);
  31. QWidget * widget = ui. listWidget ->itemWidget ( item);
  32. QCheckBox * box = ( QCheckBox*) widget ;
  33. QString text = box-> text ();
  34. box ->setChecked ( true);
  35. }
  36. }
  37. void icDisplayModel :: selectNone()
  38. {
  39. int count = ui. listWidget ->count ();
  40. for (int i=0; i <count ; i++)
  41. {
  42. QListWidgetItem * item = ui. listWidget ->item ( i);
  43. QWidget * widget = ui. listWidget ->itemWidget ( item);
  44. QCheckBox * box = ( QCheckBox*) widget ;
  45. QString text = box-> text ();
  46. box ->setChecked ( false);
  47. }
  48. }
  49. bool icDisplayModel :: start()
  50. {
  51. show ();
  52. return true ;
  53. }
  54. void icDisplayModel :: initListWidget( QString description )
  55. {
  56. insertListWidget (description );
  57. }
  58. bool icDisplayModel :: insertListWidget( QString description )
  59. {
  60. QListWidgetItem * item = new QListWidgetItem ();
  61. //QString str = "box" + QString::number(code);
  62. QCheckBox * box = new QCheckBox (description );
  63. box ->setCheckable ( true);
  64. ui .listWidget -> addItem( item );
  65. ui .listWidget -> setItemWidget( item ,box );
  66. return true ;
  67. }
  68. void icDisplayModel :: getCheckedState()
  69. {
  70. int count = ui. listWidget ->count ();
  71. bool isChecked = true;
  72. vector <bool > vIsChecked;
  73. vIsChecked .clear ();
  74. for (int i=0; i <count ; i++)
  75. {
  76. QListWidgetItem * item = ui. listWidget ->item ( i);
  77. QWidget * widget = ui. listWidget ->itemWidget ( item);
  78. QCheckBox * box = ( QCheckBox*) widget ;
  79. QString text = box-> text ();
  80. isChecked = box -> isChecked();
  81. vIsChecked .push_back ( isChecked);
  82. }
  83. }

运行结果


你可能感兴趣的:(QT)