歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> QT:在QTableView中使用各種自定義委托

QT:在QTableView中使用各種自定義委托

日期:2017/3/1 10:14:37   编辑:Linux編程

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. class ReadOnlyDelegate : public QItemDelegate
  5. {
  6. Q_OBJECT
  7. public:
  8. ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }
  9. QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option,
  10. const QModelIndex &index) const
  11. {
  12. return NULL;
  13. }
  14. };
  15. //ID列,只能輸入1-12個數字
  16. //利用QLineEdit委托和正則表達式對輸入進行限制
  17. class UserIDDelegate : public QItemDelegate
  18. {
  19. Q_OBJECT
  20. public:
  21. UserIDDelegate(QObject *parent = 0): QItemDelegate(parent) { }
  22. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  23. const QModelIndex &index) const
  24. {
  25. QLineEdit *editor = new QLineEdit(parent);
  26. QRegExp regExp("[0-9]{0,10}");
  27. editor->setValidator(new QRegExpValidator(regExp, parent));
  28. return editor;
  29. }
  30. void setEditorData(QWidget *editor, const QModelIndex &index) const
  31. {
  32. QString text = index.model()->data(index, Qt::EditRole).toString();
  33. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
  34. lineEdit->setText(text);
  35. }
  36. void setModelData(QWidget *editor, QAbstractItemModel *model,
  37. const QModelIndex &index) const
  38. {
  39. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
  40. QString text = lineEdit->text();
  41. model->setData(index, text, Qt::EditRole);
  42. }
  43. void updateEditorGeometry(QWidget *editor,
  44. const QStyleOptionViewItem &option, const QModelIndex &index) const
  45. {
  46. editor->setGeometry(option.rect);
  47. }
  48. };
  49. //年齡列,利用QSpinBox委托進行輸入限制,只能輸入1-100之間的數字
  50. class AgeDelegate : public QItemDelegate
  51. {
  52. Q_OBJECT
  53. public:
  54. AgeDelegate(QObject *parent = 0): QItemDelegate(parent) { }
  55. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  56. const QModelIndex &index) const
  57. {
  58. QSpinBox *editor = new QSpinBox(parent);
  59. editor->setMinimum(1);
  60. editor->setMaximum(100);
  61. return editor;
  62. }
  63. void setEditorData(QWidget *editor, const QModelIndex &index) const
  64. {
  65. int value = index.model()->data(index, Qt::EditRole).toInt();
  66. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
  67. spinBox->setValue(value);
  68. }
  69. void setModelData(QWidget *editor, QAbstractItemModel *model,
  70. const QModelIndex &index) const
  71. {
  72. QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
  73. spinBox->interpretText();
  74. int value = spinBox->value();
  75. model->setData(index, value, Qt::EditRole);
  76. }
  77. void updateEditorGeometry(QWidget *editor,
  78. const QStyleOptionViewItem &option, const QModelIndex &index) const
  79. {
  80. editor->setGeometry(option.rect);
  81. }
  82. };
  83. //性別列,利用QComboBox委托對輸入進行限制
  84. //這一列的單元格只能輸入Male或Female
  85. class SexDelegate : public QItemDelegate
  86. {
  87. Q_OBJECT
  88. public:
  89. SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }
  90. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
  91. const QModelIndex &index) const
  92. {
  93. QComboBox *editor = new QComboBox(parent);
  94. editor->addItem("Female");
  95. editor->addItem("Male");
  96. return editor;
  97. }
  98. void setEditorData(QWidget *editor, const QModelIndex &index) const
  99. {
  100. QString text = index.model()->data(index, Qt::EditRole).toString();
  101. QComboBox *comboBox = static_cast<QComboBox*>(editor);
  102. int tindex = comboBox->findText(text);
  103. comboBox->setCurrentIndex(tindex);
  104. }
  105. void setModelData(QWidget *editor, QAbstractItemModel *model,
  106. const QModelIndex &index) const
  107. {
  108. QComboBox *comboBox = static_cast<QComboBox*>(editor);
  109. QString text = comboBox->currentText();
  110. model->setData(index, text, Qt::EditRole);
  111. }
  112. void updateEditorGeometry(QWidget *editor,
  113. const QStyleOptionViewItem &option, const QModelIndex &index) const
  114. {
  115. editor->setGeometry(option.rect);
  116. }
  117. };
  118. //頭像列,只是在單元格中央放一張小圖而已
  119. class IconDelegate : public QItemDelegate
  120. {
  121. Q_OBJECT
  122. public:
  123. IconDelegate(QObject *parent = 0): QItemDelegate(parent) { }
  124. void paint(QPainter *painter, const QStyleOptionViewItem &option,
  125. const QModelIndex & index ) const
  126. {
  127. //show.bmp是在工程目錄中的一張圖片(其實就是QQ的圖標啦,呵呵)
  128. QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);
  129. qApp->style()->drawItemPixmap(painter, option.rect, Qt::AlignCenter, QPixmap(pixmap));
  130. }
  131. };
  132. //代理類,把所有單元格中的字符居中顯示
  133. class VIPModel : public QStandardItemModel
  134. {
  135. Q_OBJECT
  136. public:
  137. VIPModel(QObject *parent=NULL) : QStandardItemModel(parent) { }
  138. VIPModel(int row, int column, QObject *parent=NULL)
  139. : QStandardItemModel(row, column, parent) { }
  140. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
  141. {
  142. if( Qt::TextAlignmentRole == role )
  143. return Qt::AlignCenter;
  144. return QStandardItemModel::data(index, role);
  145. }
  146. };
  147. #include "main.moc"
  148. int main(int argc, char *argv[])
  149. {
  150. QApplication app(argc, argv);
  151. VIPModel *model = new VIPModel(5, 5);
  152. QTableView *tableView = new QTableView;
  153. //把表格的背景調成黃藍相間
  154. //這種方法是在網上看到的,用起來還真方便啊
  155. tableView->setAlternatingRowColors(true);
  156. tableView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);"
  157. "alternate-background-color: rgb(141, 163, 215);}");
  158. tableView->setWindowTitle("VIP List");
  159. tableView->resize(700, 400);
  160. tableView->setModel(model);
  161. QStringList headerList;
  162. headerList << "No." << "ID" << "Name" << "Age" << "Sex" << "Show";
  163. model->setHorizontalHeaderLabels(headerList);
  164. tableView->verticalHeader()->setVisible(false);
  165. tableView->horizontalHeader()->setStretchLastSection(true);
  166. //為每一列加載委托
  167. ReadOnlyDelegate readOnlyDelegate;
  168. tableView->setItemDelegateForColumn(0, &readOnlyDelegate);
  169. UserIDDelegate userIDDelegate;
  170. tableView->setItemDelegateForColumn(1, &userIDDelegate);
  171. AgeDelegate spinBoxDelegate;
  172. tableView->setItemDelegateForColumn(3, &spinBoxDelegate);
  173. SexDelegate comboBoxDelegate;
  174. tableView->setItemDelegateForColumn(4, &comboBoxDelegate);
  175. IconDelegate iconDelegate;
  176. tableView->setItemDelegateForColumn(5, &iconDelegate);
  177. for(int i=0; i<10; i++)
  178. {
  179. QModelIndex index = model->index(i, 0, QModelIndex());
  180. model->setData(index, i);
  181. }
  182. tableView->show();
  183. return app.exec();
  184. }
Copyright © Linux教程網 All Rights Reserved