《QT从基础到进阶·十》QTableView,QTableWidget,QLineEdit设置只读和输入限制

文章介绍了如何在Qt中使用QItemDelegate控制QTableView中的表格行和列设置为只读,以及通过QLineEdit的委托和正则表达式限制输入,同时展示了如何使用QSpinBox限制输入范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
};

📢博客主页: 主页
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📢本文由 梦回阑珊 原创,首发于 优快云,转载注明出处🙉
📢代码改变世界,你来改变代码!✨

QTableWidgetQt框架中的一个表格控件,用于显示编辑表格数据。如果你想要限制QTableWidget中某一列只能输入数字,可以通过以下步骤实现: 1. 创建一个自定义的QItemDelegate类,继承自QItemDelegate。 2. 在自定义的QItemDelegate类中重写createEditor()方法,用于创建一个编辑器控件。 3. 在自定义的QItemDelegate类中重写setEditorData()方法,用于设置编辑器控件的初始值。 4. 在自定义的QItemDelegate类中重写setModelData()方法,用于将编辑器控件的值保存到模型中。 5. 在自定义的QItemDelegate类中重写eventFilter()方法,用于过滤非数字输入。 6. 将自定义的QItemDelegate类应用到QTableWidget的指定列上。 下面是一个示例代码,演示如何限制QTableWidget中某一列只能输入数字: ```cpp #include <QtWidgets> class NumericDelegate : public QItemDelegate { public: QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { Q_UNUSED(option); Q_UNUSED(index); QLineEdit* editor = new QLineEdit(parent); QRegExpValidator* validator = new QRegExpValidator(QRegExp("[0-9]*"), editor); editor->setValidator(validator); return editor; } void setEditorData(QWidget* editor, const QModelIndex& index) const override { QString value = index.model()->data(index, Qt::EditRole).toString(); QLineEdit* lineEdit = static_cast<QLineEdit*>(editor); lineEdit->setText(value); } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { QLineEdit* lineEdit = static_cast<QLineEdit*>(editor); QString value = lineEdit->text(); model->setData(index, value, Qt::EditRole); } bool eventFilter(QObject* editor, QEvent* event) override { if (event->type() == QEvent::KeyPress) { QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); if (keyEvent->key() >= Qt::Key_0 && keyEvent->key() <= Qt::Key_9) { return false; // 允许输入数字 } else { return true; // 禁止输入其他字符 } } return false; } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); QTableWidget tableWidget(3, 3); tableWidget.setItemDelegateForColumn(1, new NumericDelegate); // 将自定义的QItemDelegate应用到第1列 tableWidget.show(); return app.exec(); } ``` 在上述示例代码中,我们创建了一个名为NumericDelegate的自定义QItemDelegate类。在createEditor()方法中,我们创建了一个QLineEdit作为编辑器控件,并使用QRegExpValidator设置了只能输入数字的限制。在eventFilter()方法中,我们过滤了非数字的按键事件。 你可以根据自己的需求修改示例代码,并将自定义的QItemDelegate类应用到QTableWidget的指定列上,以实现限制输入数字的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦回阑珊

一毛不嫌多,一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值