检测何时将项目添加到QTableWidget

检测何时将项目添加到QTableWidget,qt,qtablewidget,Qt,Qtablewidget,是否有任何方法可以检测项目何时添加到QTableWidget?当项目的大小和数量发生变化时,是否会发出任何信号?AQTableWidget是一个方便的小部件,它将表视图和内置模型捆绑在一起。由于模型是-aqabstractemmodel,因此可以使用rowsInserted信号获取添加行的通知,使用dataChanged信号获取行中新数据的通知(行最初为空): //https://github.com/KubaO/stackoverflown/tree/master/questions/tab

是否有任何方法可以检测项目何时添加到QTableWidget?当项目的大小和数量发生变化时,是否会发出任何信号?

A
QTableWidget
是一个方便的小部件,它将表视图和内置模型捆绑在一起。由于模型是-a
qabstractemmodel
,因此可以使用
rowsInserted
信号获取添加行的通知,使用
dataChanged
信号获取行中新数据的通知(行最初为空):

//https://github.com/KubaO/stackoverflown/tree/master/questions/tablewidget-add-34925650
#包括
int main(int argc,字符**argv){
类型定义QObject Q;
QApplication app{argc,argv};
qw;
QVBoxLayout布局{&w};
QTableWidget表;
QLabel消息1、消息2;
QPushButton按钮{“添加项”};
layout.addWidget(&table);
layout.addWidget(&message1);
layout.addWidget(&message2);
layout.addWidget(&按钮);
w、 show();
表3.setColumnCount(1);
Q::connect(&button,&QPushButton::单击,&table,[&table]{
auto r=table.rowCount();
自动项目=新的QTableWidgetItem(QStringLiteral(“项目%1”).arg(r+1));
表.插入行(r);
表.setItem(r,0,item);
});
Q::connect(table.model(),&QAbstractItemModel::rowsInserted,&message1,
[&](常量QModelIndex&,int first,int last){
message1.setText(QStringLiteral(“插入的行%1:%2”).arg(第一个).arg(最后一个));
});
Q::connect(table.model(),&QAbstractItemModel::dataChanged,&message2,
[&](常量QModelIndex和左上角,常量QModelIndex和,常量QVector和){
message2.setText(QStringLiteral(“新数据:\%1\”).arg(topLeft.data().toString());
});
返回app.exec();
}
// https://github.com/KubaO/stackoverflown/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();
}