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 @ 2023-10-18 18:42  雾枫  阅读(97)  评论(0编辑  收藏  举报