我有一个QTableView
并且我想验证用户输入。如果用户在 的单元格中插入无效值QTableView
,我想突出显示该单元格并禁用QPushButton
.
我怎样才能做到这一点?我可以使用QValidator
吗?
我有一个QTableView
并且我想验证用户输入。如果用户在 的单元格中插入无效值QTableView
,我想突出显示该单元格并禁用QPushButton
.
我怎样才能做到这一点?我可以使用QValidator
吗?
是的,您可以这样做,为此使用自定义QItemDelegate
(我QIntValidator
只是作为示例使用)。
标题:
#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H
#include <QItemDelegate>
class ItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ItemDelegate(QObject *parent = 0);
protected:
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget * editor, const QModelIndex & index) const;
void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
signals:
public slots:
};
#endif // ITEMDELEGATE_H
Cpp
#include "itemdelegate.h"
#include <QLineEdit>
#include <QIntValidator>
ItemDelegate::ItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
QWidget *ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
editor->setValidator(new QIntValidator);
return editor;
}
void ItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value =index.model()->data(index, Qt::EditRole).toString();
QLineEdit *line = static_cast<QLineEdit*>(editor);
line->setText(value);
}
void ItemDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index, value);
}
void ItemDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
用法:
#include "itemdelegate.h"
//...
ItemDelegate *itDelegate = new ItemDelegate;
ui->tableView->setItemDelegate(itDelegate);
在这种情况下,用户将无法输入错误的数据,但您可以使用下一个:
void ItemDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *line = static_cast<QLineEdit*>(editor);
QIntValidator validator;
int pos = 0;
QString data = line->text();
if(validator.validate(data,pos) != QValidator::Acceptable)
{
qDebug() << "not valid";//do something
}
else
{
model->setData(index, data);
}
}
但在这种情况下,不要忘记editor->setValidator(new QIntValidator);
从代码中删除行
我完成了 Kosovan 答案的 PyQt/PySide 实现。我在这里为那些不使用 C++ 的人提供了 python 代码:
from PySide2 import QtWidgets, QtCore, QtGui
class ItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent):
super().__init__(parent)
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
editor.setValidator(QtGui.QIntValidator())
return editor
def setEditorData(self, editor, index):
value = str(index.model()._data[index.row()][index.column()])
editor.setText(value)
def setModelData(self, editor, model, index):
model.setData(index, editor.text(), QtCore.Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return str(self._data[index.row()][index.column()])
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
return len(self._data[0])
def setData(self, index, value, role):
if role == QtCore.Qt.EditRole:
try:
value = int(value)
except ValueError:
return False
self._data[index.row()][index.column()] = value
return True
return False
def flags(self, index):
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
data = [
[1, 2],
[3, 4],
[5, 6],
]
self.model = TableModel(data)
self.table = QtWidgets.QTableView()
self.table.setModel(self.model)
self.table.setItemDelegate(ItemDelegate(self))
self.setCentralWidget(self.table)
if __name__ == '__main__':
app = QtWidgets.QApplication()
win = MainWindow()
win.show()
app.exec_()