QTableWidget指定某行某列只能输入数字
主要通过给QTableWidget设置代理,即继承QItemDelegate类重写其中的方法实现,具体代码如下:
1 /** 2 * @file NonNegativeNumberItemDelegate.h 3 * @brief 非负整数校验代理 4 * @author 禅元天道 (chanyuantiandao@126.com) 5 * @date 2024-01-24 6 */ 7 #ifndef NONNEGATIVENUMBERITEMDELEGATE_H 8 #define NONNEGATIVENUMBERITEMDELEGATE_H 9 10 #include <QItemDelegate> 11 12 /** 13 * @brief 非负整数校验代理类 14 * @author 禅元天道 (chanyuantiandao@126.com) 15 * @date 2024-01-24 16 */ 17 class NonNegativeNumberItemDelegate : public QItemDelegate 18 { 19 public: 20 NonNegativeNumberItemDelegate(QObject* parent = nullptr); 21 /** 22 * @brief 创建编辑器. 23 * @author 禅元天道 (chanyuantiandao@126.com) 24 * @date 2024-01-24 25 */ 26 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; 27 /** 28 * @brief 设置编辑器的值. 29 * @author 禅元天道 (chanyuantiandao@126.com) 30 * @date 2024-01-24 31 */ 32 void setEditorData(QWidget *editor, const QModelIndex &index) const override; 33 /** 34 * @brief 设置控件对应单元格的值. 35 * @author 禅元天道 (chanyuantiandao@126.com) 36 * @date 2024-01-24 37 */ 38 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; 39 /** 40 * @brief 设置控件的属性. 41 * @author 禅元天道 (chanyuantiandao@126.com) 42 * @date 2024-01-24 43 */ 44 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; 45 }; 46 47 #endif // NONNEGATIVENUMBERITEMDELEGATE_H
1 /** 2 * @file NonNegativeNumberItemDelegate.cpp 3 * @brief 非负整数校验代理 4 * @author 禅元天道 (chanyuantiandao@126.com) 5 * @date 2024-01-24 6 */ 7 #include "NonNegativeNumberItemDelegate.h" 8 #include <QLineEdit> 9 #include <QIntValidator> 10 #include <QModelIndex> 11 #include <QAbstractItemModel> 12 13 14 NonNegativeNumberItemDelegate::NonNegativeNumberItemDelegate(QObject* parent) : QItemDelegate(parent) 15 { 16 17 } 18 /** 19 * @brief 创建编辑器. 20 * @author 禅元天道 (chanyuantiandao@126.com) 21 * @date 2024-01-24 22 */ 23 QWidget *NonNegativeNumberItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 24 { 25 QLineEdit* editor = new QLineEdit(parent); 26 editor->setStyleSheet("border:0px;"); 27 QIntValidator* intValidator = new QIntValidator(editor); 28 intValidator->setBottom(0); 29 editor->setValidator(intValidator); 30 return editor; 31 } 32 /** 33 * @brief 设置编辑器的值. 34 * @author 禅元天道 (chanyuantiandao@126.com) 35 * @date 2024-01-24 36 */ 37 void NonNegativeNumberItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 38 { 39 QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor); 40 QString value = index.model()->data(index).toString(); 41 lineEdit->setText(value); 42 } 43 /** 44 * @brief 设置控件对应单元格的值. 45 * @author 禅元天道 (chanyuantiandao@126.com) 46 * @date 2024-01-24 47 */ 48 void NonNegativeNumberItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 49 { 50 QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor); 51 QString text = lineEdit->text().trimmed(); 52 while (text.startsWith("0") && text != "0") 53 { 54 text = text.mid(1); 55 } 56 model->setData(index, text); 57 } 58 /** 59 * @brief 设置控件的属性. 60 * @author 禅元天道 (chanyuantiandao@126.com) 61 * @date 2024-01-24 62 */ 63 void NonNegativeNumberItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const 64 { 65 editor->setGeometry(option.rect); 66 }
使用方法:
1 NonNegativeNumberItemDelegate* itemDelegate = new NonNegativeNumberItemDelegate; 2 _ui->tableWidget->setItemDelegateForColumn(1, itemDelegate); 3 _ui->tableWidget->setItemDelegateForRow(1, itemDelegate);