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,将表格的背景色弄成黄蓝相间。
截图:
上代码:
- #include<QtGui>
- //编号列,只读委托
- //这个方法我还真想不到,呵呵
- classReadOnlyDelegate:publicQItemDelegate
- {
- Q_OBJECT
- public:
- ReadOnlyDelegate(QObject*parent=0):QItemDelegate(parent){}
- QWidget*createEditor(QWidget*parent,constQStyleOptionViewItem&option,
- constQModelIndex&index)const
- {
- returnNULL;
- }
- };
- //ID列,只能输入1-12个数字
- //利用QLineEdit委托和正则表达式对输入进行限制
- classUserIDDelegate:publicQItemDelegate
- {
- Q_OBJECT
- public:
- UserIDDelegate(QObject*parent=0):QItemDelegate(parent){}
- QWidget*createEditor(QWidget*parent,
- constQModelIndex&index)const
- {
- QLineEdit*editor=newQLineEdit(parent);
- QRegExpregExp("[0-9]{0,10}");
- editor->setValidator(newQRegExpValidator(regExp,parent));
- returneditor;
- }
- voidsetEditorData(QWidget*editor,constQModelIndex&index)const
- {
- QStringtext=index.model()->data(index,Qt::EditRole).toString();
- QLineEdit*lineEdit=static_cast<QLineEdit*>(editor);
- lineEdit->setText(text);
- }
- voidsetModelData(QWidget*editor,QAbstractItemModel*model,
- constQModelIndex&index)const
- {
- QLineEdit*lineEdit=static_cast<QLineEdit*>(editor);
- QStringtext=lineEdit->text();
- model->setData(index,text,Qt::EditRole);
- }
- voidupdateEditorGeometry(QWidget*editor,
- constQStyleOptionViewItem&option,constQModelIndex&index)const
- {
- editor->setGeometry(option.rect);
- }
- };
- //年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字
- classAgeDelegate:publicQItemDelegate
- {
- Q_OBJECT
- public:
- AgeDelegate(QObject*parent=0):QItemDelegate(parent){}
- QWidget*createEditor(QWidget*parent,
- constQModelIndex&index)const
- {
- QSpinBox*editor=newQSpinBox(parent);
- editor->setMinimum(1);
- editor->setMaximum(100);
- returneditor;
- }
- voidsetEditorData(QWidget*editor,constQModelIndex&index)const
- {
- intvalue=index.model()->data(index,Qt::EditRole).toInt();
- QSpinBox*spinBox=static_cast<QSpinBox*>(editor);
- spinBox->setValue(value);
- }
- voidsetModelData(QWidget*editor,
- constQModelIndex&index)const
- {
- QSpinBox*spinBox=static_cast<QSpinBox*>(editor);
- spinBox->interpretText();
- intvalue=spinBox->value();
- model->setData(index,value,Qt::EditRole);
- }
- voidupdateEditorGeometry(QWidget*editor,
- constQStyleOptionViewItem&option,constQModelIndex&index)const
- {
- editor->setGeometry(option.rect);
- }
- };
- //性别列,利用QComboBox委托对输入进行限制
- //这一列的单元格只能输入Male或Female
- classSexDelegate:publicQItemDelegate
- {
- Q_OBJECT
- public:
- SexDelegate(QObject*parent=0):QItemDelegate(parent){}
- QWidget*createEditor(QWidget*parent,
- constQModelIndex&index)const
- {
- QComboBox*editor=newQComboBox(parent);
- editor->addItem("Female");
- editor->addItem("Male");
- returneditor;
- }
- voidsetEditorData(QWidget*editor,Qt::EditRole).toString();
- QComboBox*comboBox=static_cast<QComboBox*>(editor);
- inttindex=comboBox->findText(text);
- comboBox->setCurrentIndex(tindex);
- }
- voidsetModelData(QWidget*editor,
- constQModelIndex&index)const
- {
- QComboBox*comboBox=static_cast<QComboBox*>(editor);
- QStringtext=comboBox->currentText();
- model->setData(index,constQModelIndex&index)const
- {
- editor->setGeometry(option.rect);
- }
- };
- //头像列,只是在单元格中央放一张小图而已
- classIconDelegate:publicQItemDelegate
- {
- Q_OBJECT
- public:
- IconDelegate(QObject*parent=0):QItemDelegate(parent){}
- voidpaint(QPainter*painter,
- constQModelIndex&index)const
- {
- //show.bmp是在工程目录中的一张图片(其实就是QQ的图标啦,呵呵)
- QPixmappixmap=QPixmap("show.bmp").scaled(24,24);
- qApp->style()->drawItemPixmap(painter,option.rect,Qt::AlignCenter,QPixmap(pixmap));
- }
- };
- //代理类,把所有单元格中的字符居中显示
- classVIPModel:publicQStandardItemModel
- {
- Q_OBJECT
- public:
- VIPModel(QObject*parent=NULL):QStandardItemModel(parent){}
- VIPModel(introw,intcolumn,QObject*parent=NULL)
- :QStandardItemModel(row,column,parent){}
- QVariantdata(constQModelIndex&index,introle=Qt::DisplayRole)const
- {
- if(Qt::TextAlignmentRole==role)
- returnQt::AlignCenter;
- returnQStandardItemModel::data(index,role);
- }
- };
- #include"main.moc"
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- VIPModel*model=newVIPModel(5,5);
- QTableView*tableView=newQTableView;
- //把表格的背景调成黄蓝相间
- //这种方法是在网上看到的,用起来还真方便啊
- tableView->setAlternatingRowColors(true);
- tableView->setStyleSheet("QTableView{background-color:rgb(250,115);"
- "alternate-background-color:rgb(141,163,215);}");
- tableView->setWindowTitle("VIPList");
- tableView->resize(700,400);
- tableView->setModel(model);
- QStringListheaderList;
- headerList<<"No."<<"ID"<<"Name"<<"Age"<<"Sex"<<"Show";
- model->setHorizontalHeaderLabels(headerList);
- tableView->verticalHeader()->setVisible(false);
- tableView->horizontalHeader()->setStretchLastSection(true);
- //为每一列加载委托
- ReadOnlyDelegatereadOnlyDelegate;
- tableView->setItemDelegateForColumn(0,&readOnlyDelegate);
- UserIDDelegateuserIDDelegate;
- tableView->setItemDelegateForColumn(1,&userIDDelegate);
- AgeDelegatespinBoxDelegate;
- tableView->setItemDelegateForColumn(3,&spinBoxDelegate);
- SexDelegatecomboBoxDelegate;
- tableView->setItemDelegateForColumn(4,&comboBoxDelegate);
- IconDelegateiconDelegate;
- tableView->setItemDelegateForColumn(5,&iconDelegate);
- for(inti=0;i<10;i++)
- {
- QModelIndexindex=model->index(i,QModelIndex());
- model->setData(index,i);
- }
- tableView->show();
- returnapp.exec();
- }
转自:http://www.linuxidc.com/Linux/2012-07/66820.htm
原文链接:https://www.f2er.com/regex/360774.html