Qt 表格设置表格单元的输入正则表达式

// 用于给表格设置正则表达式,
        // table视图 QTableView
        // table模型 QStandardItemModel
        // 使用示例:
        //          wangchuan::RegExp::InputDelegate* inputDelegate = new wangchuan::RegExp::InputDelegate;
        //          m_tableview->setItemDelegateForColumn(0, inputDelegate); // 设置索引0列的输入正则表达式
        class InputDelegate:public QStyledItemDelegate
        {
        public:
            QWidget *createEditor(QWidget*parent,const QStyleOptionViewItem &option,const QModelIndex &index)const override
            {
                QLineEdit *editor =new QLineEdit(parent);
                QRegExp regex("[^\\s]+$"); // 可以替换为你想要的正则表达式
                QValidator *validator =new QRegExpValidator(regex,parent);
                editor->setValidator(validator);
                return editor;
            }
        };
posted @   雾枫  阅读(128)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
点击右上角即可分享
微信分享提示