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

另一种参考:结合用

qt-3: Qtableview中设定指定列只读。

在Qtableview中没有设置列只读的函数,因此可以考虑使用QItemDelegatel来实现。

创建一个类readonlyDelegate,继承QItemDelegate

头文件
//readonlydelegate.h
#ifndef READONLYDELEGATE_H
#define READONLYDELEGATE_H

#include <QItemDelegate>

class readonlyDelegate : public QItemDelegate
{
public:
readonlyDelegate();
readonlyDelegate(QObject* parent = 0) : QItemDelegate(parent){}
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};

#endif // READONLYDELEGATE_Hcpp文件 。在cpp文件中,只要重写createEditor这个方法就行了。
//readonlydelegate.cpp
#include "readonlydelegate.h"

QWidget *ReadonlyDelegate::createEditor(QWidget *parent,
                    const QStyleOptionViewItem &option,
                    const QModelIndex &index) const
{

    return NULL;
}
类创建好以后就可以使用了。使用的方法比较简单,下面是一个简单的例子,指定地2列为只读。
ui->tableView->setItemDelegateForColumn(2,new ReadonlyDelegate(this));
或tableView->setItemDelegateForColumn(2,new ReadonlyDelegate(this));

猜你喜欢

转载自blog.csdn.net/weixin_39568531/article/details/79929333