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

来源:互联网 发布:oracle数据备份与恢复 编辑:程序博客网 时间:2024/05/11 14:31

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. }  
0 0
原创粉丝点击