PDA

View Full Version : Populate QTableWidget



ruben.rodrigues
3rd May 2011, 13:21
Hi all!

I need to populate a tablewidget from a datebase but I can't seem to do it. I have more experience with treewidgets but for this project I want tablewidgets.

I still don't have much code because I can't do the basic, like add a row..
The QTableWidget was added directly from the tool box and has 7 Columns and 0 Rows as default.

I looked up some examples but they didn't work. Is this wrong:



QLineEdit *test = new QLineEdit;
QTableWidgetItem *item = new QTableWidgetItem;

item->setText("test");

int n = ui->tableWidget->rowCount();
ui->tableWidget->setRowCount(n);
ui->tableWidget->setItem(n,0,item);
ui->tableWidget->setCellWidget(n,1,test);

thanks!

qlands
3rd May 2011, 13:58
Hi,
Modify this line: ui->tableWidget->setRowCount(n+1);
Add this line after it: ui->tableWidget->setColumnCount(2);

n at the beginning is 0 (no lines) and you don't have columns. Column or row count will be 1 and each index with be 0.

Carlos.

MasterBLB
3rd May 2011, 14:21
If you want to display a table from a database,then the better way is to use QTableView as view and QSqlTableModel as model.