2016-01-21 96 views

回答

2

A QTableWidget是一個方便的小部件,它將表格視圖和內置模型捆綁在一起。由於模型是,一個QAbstractItemModel,您可以使用rowsInserted信號得到一個添加的行的通知,和dataChanged信號得到一個新的數據的通知在一排(行最初是空的):

screenshot

// 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(); 
} 
相關問題