检测何时将项目添加到 QTableWidget
Detect when an item is added to a QTableWidget
有什么方法可以检测何时将项目添加到 QTableWidget 吗?当大小,项目数量发生变化时是否有任何信号发出?
QTableWidget
是一个方便的小部件,它将 table 视图和 built-in 模型捆绑在一起。由于模型 is-a QAbstractItemModel
,您可以使用 rowsInserted
信号获取添加行的通知,并使用 dataChanged
信号获取行中新数据的通知(该行最初为空):
// https://github.com/KubaO/Whosebugn/tree/master/questions/tablewidget-add-34925650
#include <QtWidgets>
int main(int argc, char ** argv) {
typedef QObject Q;
QApplication app{argc, argv};
QWidget w;
QVBoxLayout layout{&w};
QTableWidget table;
QLabel message1, message2;
QPushButton button{"Add Item"};
layout.addWidget(&table);
layout.addWidget(&message1);
layout.addWidget(&message2);
layout.addWidget(&button);
w.show();
table.setColumnCount(1);
Q::connect(&button, &QPushButton::clicked, &table, [&table]{
auto r = table.rowCount();
auto item = new QTableWidgetItem(QStringLiteral("Item %1").arg(r+1));
table.insertRow(r);
table.setItem(r, 0, item);
});
Q::connect(table.model(), &QAbstractItemModel::rowsInserted, &message1,
[&](const QModelIndex &, int first, int last){
message1.setText(QStringLiteral("Rows inserted %1:%2").arg(first).arg(last));
});
Q::connect(table.model(), &QAbstractItemModel::dataChanged, &message2,
[&](const QModelIndex & topLeft, const QModelIndex &, const QVector<int>&){
message2.setText(QStringLiteral("New data: \"%1\"").arg(topLeft.data().toString()));
});
return app.exec();
}
有什么方法可以检测何时将项目添加到 QTableWidget 吗?当大小,项目数量发生变化时是否有任何信号发出?
QTableWidget
是一个方便的小部件,它将 table 视图和 built-in 模型捆绑在一起。由于模型 is-a QAbstractItemModel
,您可以使用 rowsInserted
信号获取添加行的通知,并使用 dataChanged
信号获取行中新数据的通知(该行最初为空):
// https://github.com/KubaO/Whosebugn/tree/master/questions/tablewidget-add-34925650
#include <QtWidgets>
int main(int argc, char ** argv) {
typedef QObject Q;
QApplication app{argc, argv};
QWidget w;
QVBoxLayout layout{&w};
QTableWidget table;
QLabel message1, message2;
QPushButton button{"Add Item"};
layout.addWidget(&table);
layout.addWidget(&message1);
layout.addWidget(&message2);
layout.addWidget(&button);
w.show();
table.setColumnCount(1);
Q::connect(&button, &QPushButton::clicked, &table, [&table]{
auto r = table.rowCount();
auto item = new QTableWidgetItem(QStringLiteral("Item %1").arg(r+1));
table.insertRow(r);
table.setItem(r, 0, item);
});
Q::connect(table.model(), &QAbstractItemModel::rowsInserted, &message1,
[&](const QModelIndex &, int first, int last){
message1.setText(QStringLiteral("Rows inserted %1:%2").arg(first).arg(last));
});
Q::connect(table.model(), &QAbstractItemModel::dataChanged, &message2,
[&](const QModelIndex & topLeft, const QModelIndex &, const QVector<int>&){
message2.setText(QStringLiteral("New data: \"%1\"").arg(topLeft.data().toString()));
});
return app.exec();
}