QTableWidgetItem* ti; connectTable->setRowCount(connectTable->rowCount()+1); for (int i=0; i<6; i++) { ti = new QTableWidgetItem(""); connectTable->setItem(connectTable->rowCount()-1,i,ti); }
#include <QtGui>class OptionDialog : public QDialog { Q_OBJECT public: OptionDialog(); public slots: void tableEdit(int, int);};
#include <QtGui>#include "main.h"OptionDialog::OptionDialog() { QTableWidgetItem* ti; QVBoxLayout* vl = new QVBoxLayout; setLayout(vl); QTableWidget* connectTable = new QTableWidget(1,6); vl->addWidget(connectTable); for (int i=0; i<6; i++) { ti = new QTableWidgetItem(""); connectTable->setItem(0,i,ti); } connect(connectTable,SIGNAL(cellChanged(int, int)),SLOT(tableEdit(int, int)));}void OptionDialog::tableEdit(int row, int col) { QTableWidget* connectTable = qobject_cast<QTableWidget*>(sender()); int count=0; QTableWidgetItem* ti; for (int i=0; i<4; i++) if (connectTable->item(connectTable->rowCount()-1,i)->text()!="") count+=1; if (count==4) { connectTable->setRowCount(connectTable->rowCount()+1); for (int i=0; i<6; i++) { ti = new QTableWidgetItem(""); connectTable->setItem(connectTable->rowCount()-1,i,ti); } }}int main(int argc, char *argv[]) { QApplication app(argc, argv); OptionDialog mainWin; mainWin.show(); return app.exec();}
C++ (Qt)for (int i=0; i<4; i++) if (connectTable->item(connectTable->rowCount()-1,i)) if (connectTable->item(connectTable->rowCount()-1,i)->text()!="") count+=1;