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));
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QtQTableView添加复选框可以通过自定义一个QItemDelegate来实现。下面是一个简单的示例: 首先,我们定义一个继承自QItemDelegate的自定义委托类,名称为CheckBoxDelegate: ```cpp class CheckBoxDelegate : public QItemDelegate { public: CheckBoxDelegate(QObject* parent = nullptr) : QItemDelegate(parent) { } void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { QStyleOptionButton checkBoxOption; checkBoxOption.rect = option.rect; checkBoxOption.state = index.data(Qt::CheckStateRole).toBool() ? QStyle::State_On : QStyle::State_Off; QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter); } else { QItemDelegate::paint(painter, option, index); } } QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { QCheckBox* checkBox = new QCheckBox(parent); return checkBox; } else { return QItemDelegate::createEditor(parent, option, index); } } void setEditorData(QWidget* editor, const QModelIndex& index) const override { if (index.column() == 0) { bool checked = index.data(Qt::CheckStateRole).toBool(); QCheckBox* checkBox = static_cast<QCheckBox*>(editor); checkBox->setChecked(checked); } else { QItemDelegate::setEditorData(editor, index); } } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { if (index.column() == 0) { QCheckBox* checkBox = static_cast<QCheckBox*>(editor); model->setData(index, checkBox->isChecked(), Qt::CheckStateRole); } else { QItemDelegate::setModelData(editor, model, index); } } }; ``` 然后,在我们的QTableView使用这个自定义委托类来实现复选框的功能: ```cpp QTableView* tableView = new QTableView; QStandardItemModel* model = new QStandardItemModel; // 设置表格大小 model->setRowCount(3); model->setColumnCount(2); // 设置表头 model->setHeaderData(0, Qt::Horizontal, "复选框列"); model->setHeaderData(1, Qt::Horizontal, "其他列"); // 设置复选框数据 model->setData(model->index(0, 0), Qt::Checked, Qt::CheckStateRole); model->setData(model->index(1, 0), Qt::Unchecked, Qt::CheckStateRole); model->setData(model->index(2, 0), Qt::Checked, Qt::CheckStateRole); // 设置委托 CheckBoxDelegate* checkBoxDelegate = new CheckBoxDelegate(tableView); tableView->setItemDelegateForColumn(0, checkBoxDelegate); // 设置数据模型 tableView->setModel(model); ``` 上述代码创建了一个QTableView和一个QStandardItemModel,并通过数据模型设置了3行2列的数据。使用定义委托类CheckBoxDelegate将第一列的数据设置为复选框。然后将数据模型设置到QTableView。 最后,我们通过`tableView->setModel(model)`将数据模型设置到QTableView完成复选框的添加。 这样就可以在QTableView添加复选框了。记得在使用前先将相应的头文件导入:`#include <QCheckBox>`、`#include <QPainter>`、`#include <QStandardItemModel>`、`#include <QTableView>`、`#include <QItemDelegate>`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值