QT:在QTableView中使用各种自定义委托

前端之家收集整理的这篇文章主要介绍了QT:在QTableView中使用各种自定义委托前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

QT的MVC(View/Delegate)模型十分强大,可以利用各种控件来对表格的输入进行限制,不过我以前一直没有过,这几天研究了一下,写个小例子,希望大家喜欢。

如果看不懂这个例子,请先看QT的自带例子:http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html

思路:

1:为每一列定义委托:
A:第一列是编号列,使用只读委托,令该列的单元格是只读的
B:第三列是ID列,只能输入1-12个数字,利用QLineEdit委托和正则表达式对输入进行限制
C:第四年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字
D:第五列是性别列,利用QComboBox委托对输入进行限制,该列的单元格只能输入Male或Female
E:第六列是头像列,在该列的单元格中央放置一张头像
2:定义代理类,把所有单元格中的字符居中显示
3:利用QSS,将表格的背景色弄成黄蓝相间。

截图:

代码

  1. #include<QtGui>
  2. //编号列,只读委托
  3. //这个方法我还真想不到,呵呵
  4. classReadOnlyDelegate:publicQItemDelegate
  5. {
  6. Q_OBJECT
  7. public:
  8. ReadOnlyDelegate(QObject*parent=0):QItemDelegate(parent){}
  9. QWidget*createEditor(QWidget*parent,constQStyleOptionViewItem&option,
  10. constQModelIndex&index)const
  11. {
  12. returnNULL;
  13. }
  14. };
  15. //ID列,只能输入1-12个数字
  16. //利用QLineEdit委托和正则表达式对输入进行限制
  17. classUserIDDelegate:publicQItemDelegate
  18. {
  19. Q_OBJECT
  20. public:
  21. UserIDDelegate(QObject*parent=0):QItemDelegate(parent){}
  22. QWidget*createEditor(QWidget*parent,
  23. constQModelIndex&index)const
  24. {
  25. QLineEdit*editor=newQLineEdit(parent);
  26. QRegExpregExp("[0-9]{0,10}");
  27. editor->setValidator(newQRegExpValidator(regExp,parent));
  28. returneditor;
  29. }
  30. voidsetEditorData(QWidget*editor,constQModelIndex&index)const
  31. {
  32. QStringtext=index.model()->data(index,Qt::EditRole).toString();
  33. QLineEdit*lineEdit=static_cast<QLineEdit*>(editor);
  34. lineEdit->setText(text);
  35. }
  36. voidsetModelData(QWidget*editor,QAbstractItemModel*model,
  37. constQModelIndex&index)const
  38. {
  39. QLineEdit*lineEdit=static_cast<QLineEdit*>(editor);
  40. QStringtext=lineEdit->text();
  41. model->setData(index,text,Qt::EditRole);
  42. }
  43. voidupdateEditorGeometry(QWidget*editor,
  44. constQStyleOptionViewItem&option,constQModelIndex&index)const
  45. {
  46. editor->setGeometry(option.rect);
  47. }
  48. };
  49. //年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字
  50. classAgeDelegate:publicQItemDelegate
  51. {
  52. Q_OBJECT
  53. public:
  54. AgeDelegate(QObject*parent=0):QItemDelegate(parent){}
  55. QWidget*createEditor(QWidget*parent,
  56. constQModelIndex&index)const
  57. {
  58. QSpinBox*editor=newQSpinBox(parent);
  59. editor->setMinimum(1);
  60. editor->setMaximum(100);
  61. returneditor;
  62. }
  63. voidsetEditorData(QWidget*editor,constQModelIndex&index)const
  64. {
  65. intvalue=index.model()->data(index,Qt::EditRole).toInt();
  66. QSpinBox*spinBox=static_cast<QSpinBox*>(editor);
  67. spinBox->setValue(value);
  68. }
  69. voidsetModelData(QWidget*editor,
  70. constQModelIndex&index)const
  71. {
  72. QSpinBox*spinBox=static_cast<QSpinBox*>(editor);
  73. spinBox->interpretText();
  74. intvalue=spinBox->value();
  75. model->setData(index,value,Qt::EditRole);
  76. }
  77. voidupdateEditorGeometry(QWidget*editor,
  78. constQStyleOptionViewItem&option,constQModelIndex&index)const
  79. {
  80. editor->setGeometry(option.rect);
  81. }
  82. };
  83. //性别列,利用QComboBox委托对输入进行限制
  84. //这一列的单元格只能输入Male或Female
  85. classSexDelegate:publicQItemDelegate
  86. {
  87. Q_OBJECT
  88. public:
  89. SexDelegate(QObject*parent=0):QItemDelegate(parent){}
  90. QWidget*createEditor(QWidget*parent,
  91. constQModelIndex&index)const
  92. {
  93. QComboBox*editor=newQComboBox(parent);
  94. editor->addItem("Female");
  95. editor->addItem("Male");
  96. returneditor;
  97. }
  98. voidsetEditorData(QWidget*editor,Qt::EditRole).toString();
  99. QComboBox*comboBox=static_cast<QComboBox*>(editor);
  100. inttindex=comboBox->findText(text);
  101. comboBox->setCurrentIndex(tindex);
  102. }
  103. voidsetModelData(QWidget*editor,
  104. constQModelIndex&index)const
  105. {
  106. QComboBox*comboBox=static_cast<QComboBox*>(editor);
  107. QStringtext=comboBox->currentText();
  108. model->setData(index,constQModelIndex&index)const
  109. {
  110. editor->setGeometry(option.rect);
  111. }
  112. };
  113. //头像列,只是在单元格中央放一张小图而已
  114. classIconDelegate:publicQItemDelegate
  115. {
  116. Q_OBJECT
  117. public:
  118. IconDelegate(QObject*parent=0):QItemDelegate(parent){}
  119. voidpaint(QPainter*painter,
  120. constQModelIndex&index)const
  121. {
  122. //show.bmp是在工程目录中的一张图片(其实就是QQ的图标啦,呵呵)
  123. QPixmappixmap=QPixmap("show.bmp").scaled(24,24);
  124. qApp->style()->drawItemPixmap(painter,option.rect,Qt::AlignCenter,QPixmap(pixmap));
  125. }
  126. };
  127. //代理类,把所有单元格中的字符居中显示
  128. classVIPModel:publicQStandardItemModel
  129. {
  130. Q_OBJECT
  131. public:
  132. VIPModel(QObject*parent=NULL):QStandardItemModel(parent){}
  133. VIPModel(introw,intcolumn,QObject*parent=NULL)
  134. :QStandardItemModel(row,column,parent){}
  135. QVariantdata(constQModelIndex&index,introle=Qt::DisplayRole)const
  136. {
  137. if(Qt::TextAlignmentRole==role)
  138. returnQt::AlignCenter;
  139. returnQStandardItemModel::data(index,role);
  140. }
  141. };
  142. #include"main.moc"
  143. intmain(intargc,char*argv[])
  144. {
  145. QApplicationapp(argc,argv);
  146. VIPModel*model=newVIPModel(5,5);
  147. QTableView*tableView=newQTableView;
  148. //把表格的背景调成黄蓝相间
  149. //这种方法是在网上看到的,用起来还真方便啊
  150. tableView->setAlternatingRowColors(true);
  151. tableView->setStyleSheet("QTableView{background-color:rgb(250,115);"
  152. "alternate-background-color:rgb(141,163,215);}");
  153. tableView->setWindowTitle("VIPList");
  154. tableView->resize(700,400);
  155. tableView->setModel(model);
  156. QStringListheaderList;
  157. headerList<<"No."<<"ID"<<"Name"<<"Age"<<"Sex"<<"Show";
  158. model->setHorizontalHeaderLabels(headerList);
  159. tableView->verticalHeader()->setVisible(false);
  160. tableView->horizontalHeader()->setStretchLastSection(true);
  161. //为每一列加载委托
  162. ReadOnlyDelegatereadOnlyDelegate;
  163. tableView->setItemDelegateForColumn(0,&readOnlyDelegate);
  164. UserIDDelegateuserIDDelegate;
  165. tableView->setItemDelegateForColumn(1,&userIDDelegate);
  166. AgeDelegatespinBoxDelegate;
  167. tableView->setItemDelegateForColumn(3,&spinBoxDelegate);
  168. SexDelegatecomboBoxDelegate;
  169. tableView->setItemDelegateForColumn(4,&comboBoxDelegate);
  170. IconDelegateiconDelegate;
  171. tableView->setItemDelegateForColumn(5,&iconDelegate);
  172. for(inti=0;i<10;i++)
  173. {
  174. QModelIndexindex=model->index(i,QModelIndex());
  175. model->setData(index,i);
  176. }
  177. tableView->show();
  178. returnapp.exec();
  179. }

转自:http://www.linuxidc.com/Linux/2012-07/66820.htm
原文链接:https://www.f2er.com/regex/360774.html

猜你在找的正则表达式相关文章