1、设置QTableView表格某一行或者某一列只读
实现一个委托,不返回任何editor,把它设为只读的行列
class ReadOnlyDelegate : public QItemDelegate { public: ReadOnlyDelegate(QWidget* parent = NULL) :QItemDelegate(parent) {} QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override //final { Q_UNUSED(parent) Q_UNUSED(option) Q_UNUSED(index) return NULL; } }; ReadOnlyDelegate* readOnlyDelegate = new ReadOnlyDelegate(this); setItemDelegateForColumn(2, readOnlyDelegate); //设置某列只读 setItemDelegateForRow(0, readOnlyDelegate); //设置某行只读
2、QLineEdit输入限制
利用QLineEdit委托和正则表达式对输入进行限制
实现只能输入1-12个数字
class UserIDDelegate : public QItemDelegate { Q_OBJECT public: UserIDDelegate(QObject* parent = 0) : QItemDelegate(parent) { } QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QLineEdit* editor = new QLineEdit(parent); QRegExp regExp("[0-9]{0,10}"); editor->setValidator(new QRegExpValidator(regExp, parent)); return editor; } void setEditorData(QWidget* editor, const QModelIndex& index) const { QString text = index.model()->data(index, Qt::EditRole).toString(); QLineEdit* lineEdit = static_cast<QLineEdit*>(editor); lineEdit->setText(text); } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QLineEdit* lineEdit = static_cast<QLineEdit*>(editor); QString text = lineEdit->text(); model->setData(index, text, Qt::EditRole); } void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } };
设置表格只能输入数字:
TableWidget::TableWidget(QWidget* parent): QTableView(parent) { UserIDDelegate* userIDDelegate = new UserIDDelegate(this); setItemDelegateForRow(1, userIDDelegate); //设置某行只能输入数字 setItemDelegateForColumn(2, userIDDelegate); //设置某列只能输入数字 }
把上面委托换成下面两句可以实现只能输入double类型
QDoubleValidator* doubleValue = new QDoubleValidator();
editor->setValidator(doubleValue);
QLineEdit 一些限制输入:
(1)只输入数字,小数点和负号:
QRegExp exp("[0-9\\.-]+$"); QValidator* Validator = new QRegExpValidator(exp); ui.lineEdit->setValidator(Validator);
(2)
QRegExp regexp_0To360("^([0-9]|[1-9]\\d|[1-2]\\d{0,2}|3[0-5]\\d|360)$"); QRegExp regexp_0To100("^([0-9]|[1-9]\\d|[1][0][0])$"); QRegExp regexp_D0To100("^([0-9]|[0-9]\\.[0-9]|[1-9][0-9]|[1-9][0-9]\\.[0-9]|[1][0][0]|[1][0][0]\\.[0])$");
(3)-360.0~360.0
QRegExp exp("^-?(360|1?[0-9]?\\d(\\.\\d{1,1})?)$"); QRegExpValidator* Validator = new QRegExpValidator(exp); ui.globalrotationtext->setValidator(Validator);
3、利用QSpinBox委托进行输入限制
限制只能输入1-100之间的数字
class AgeDelegate : public QItemDelegate { Q_OBJECT public: AgeDelegate(QObject* parent = 0) : QItemDelegate(parent) { } QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QSpinBox* editor = new QSpinBox(parent); editor->setMinimum(1); editor->setMaximum(100); return editor; } void setEditorData(QWidget* editor, const QModelIndex& index) const { int value = index.model()->data(index, Qt::EditRole).toInt(); QSpinBox* spinBox = static_cast<QSpinBox*>(editor); spinBox->setValue(value); } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { QSpinBox* spinBox = static_cast<QSpinBox*>(editor); spinBox->interpretText(); int value = spinBox->value(); model->setData(index, value, Qt::EditRole); } void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } };