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

原文地址::https://blog.csdn.net/lhchen922/article/details/38367719

相关文章

1、QT:在QTableView中使用各种自定义委托----https://blog.csdn.net/zgrjkflmkyc/article/details/48086851

2、QTableView使用自定义委托(QItemDelegate)----https://www.cnblogs.com/doubleeleven/p/3948803.html

3、QTableView表头设置问题,请教 了----https://bbs.csdn.net/topics/394473026

4、Qt自定义委托在QTableView中绘制控件、图片、文字----https://blog.csdn.net/zhi379/article/details/28412189?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242

5、QT tablewidget设置表头----https://blog.csdn.net/qq_39498490/article/details/89479280?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-5.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-5.control

6、QT QTableView QTableWidget 复杂表头(多行表头) 、(冻结、固定特定的行)----https://blog.csdn.net/xiezhongyuan07/article/details/82857631?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-11.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-11.control

 

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

 

 

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值