可以使用QItemDelegate来监测和处理QTableView中表格内容的修改事件。具体步骤如下:
1.创建自定义的ItemDelegate类,继承自QItemDelegate。在其中重写createEditor()、setEditorData()和setModelData()方法,分别用于创建编辑器、设置编辑器显示的数据和将编辑器数据保存到模型中。
2.在需要监测表格内容修改事件的类中,创建QItemDelegate实例并设置给对应的QTableView对象。通过监听dataChanged信号,即可获取到修改后的数据及其对应的模型索引。
以下是示例代码:
//CustomItemDelegate.h
#include
class CustomItemDelegate : public QItemDelegate {
Q_OBJECT
public:
CustomItemDelegate(QObject *parent = nullptr);
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
signals:
void dataChanged(const QModelIndex &index);
private slots:
void commitAndCloseEditor();
};
//CustomItemDelegate.cpp
#include “CustomItemDelegate.h”
#include
CustomItemDelegate::CustomItemDelegate(QObject *parent)
: QItemDelegate(parent)
{ }
QWidget* CustomItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QLineEdit *editor = new QLineEdit(parent);
connect(editor, &QLineEdit::editingFinished, this, &CustomItemDelegate::commitAndCloseEditor);
return editor;
}
void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
}
void CustomItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
QString value = lineEdit->text();
model->setData(index, value, Qt::EditRole);
}
void CustomItemDelegate::commitAndCloseEditor() {
QLineEdit *editor = qobject_cast<QLineEdit*>(sender());
emit dataChanged(qobject_cast<QLineEdit*>(sender())->parentIndex());
commitData(editor);
closeEditor(editor, QAbstractItemDelegate::EditNextItem);
}
//MyTableView.h
#include
class MyTableView : public QTableView {
Q_OBJECT
public:
MyTableView(QWidget *parent = nullptr);
protected:
void setModel(QAbstractItemModel *model) override;
private slots:
void onDataChanged(const QModelIndex &index);
private:
CustomItemDelegate *m_delegate;
};
//MyTableView.cpp
#include “MyTableView.h”
#include
MyTableView::MyTableView(QWidget *parent)
: QTableView(parent)
{
m_delegate = new CustomItemDelegate(this);
connect(m_delegate, &CustomItemDelegate::dataChanged, this, &MyTableView::onDataChanged);
}
void MyTableView::setModel(QAbstractItemModel *model) {
QTableView::setModel(model);
for(int i=0; i<model->rowCount(); ++i){
for(int j=0; j<model->columnCount(); ++j){
QModelIndex index = model->index(i, j);
setIndexWidget(index, m_delegate->createEditor(this, {}, index));
}
}
}
void MyTableView::onDataChanged(const QModelIndex &index) {
qDebug() << "Row:" << index.row() << ", Column:" << index.column() << ", Value:" << index.data();
}
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QStandardItemModel *model = new QStandardItemModel(3, 2);
model->setData(model->index(0, 0), "A");
model->setData(model->index(0, 1), "B");
model->setData(model->index(1, 0), "C");
model->setData(model->index(1, 1), "D");
model->setData(model->index(2, 0), "E");
model->setData(model->index(2, 1), "F");
MyTableView *tableView = new MyTableView;
tableView->setModel(model);
tableView->show();
return a.exec();
}