PDA

View Full Version : QTableWidget row insertion and set width of each column?



darpan
12th October 2006, 11:37
Hi,
I placed a QTableWidget on a form and i created three columns in this table widget through QT Designer and now want to add rows in this table widget through coding.

My problems are:-
(1) How to add rows in this table widget through coding.
(2) How to set width of each column through QT Designer and through coding.

Thanks and regards

vratojr
12th October 2006, 11:48
Hi,
1) Try insertRow(),
2) If I'm not wrong you have to set the width of the horizontal header to have all the column to resize.

Simone

jpn
12th October 2006, 11:53
(1) How to add rows in this table widget through coding.

Something like this:


int row = tableWidget->rowCount(); // current row count
tableWidget->setRowCount(row+1); // add one row
// create items in all added cells
for (int col = 0; col < tableWidget->columnCount(); ++c)
{
QTableWidgetItem* newItem = new QTableWidgetItem("blaa");
tableWidget->setItem(row, col, newItem);
}




(2) How to set width of each column through QT Designer and through coding.
to access headers:

QTableView::horizontalHeader()
QTableView::verticalHeader()

to resize:

QHeaderView::resizeSection()
QHeaderView::setStretchLastSection()
QHeaderView::setResizeMode()
...

darpan
12th October 2006, 15:00
Hi,
I read the following functions but can't get how to set width of each column in QTreeWidget through QT Designer and through coding.

to access headers:
QTableView::horizontalHeader()
QTableView::verticalHeader()

to resize:
QHeaderView::resizeSection()
QHeaderView::setStretchLastSection()
QHeaderView::setResizeMode()
...

Please give some code example to set the width of QTreeWidget Column.

Thanks and Regards

jpn
12th October 2006, 15:20
I thought it was QTableWidget.. :)



#include <QHeaderView>
QTreeWidget* treeWidget = ...;
treeWidget->header()->resizeSection(0, 20); // column 0, width 20

QTableWidget* tableWidget = ...;
tableWidget->horizontalHeader()->resizeSection(1, 50); // column 1, width 50
tableWidget->verticalHeader()->resizeSection(2, 15); // row 2, height 15

darpan
13th October 2006, 06:38
Hi,
First of all sorry , you are right, it was QTableWidget. I had to set width of Each column of QTable Widget.
I also have requirement to delete all rows created through following lines:-
row = tableWidget->rowCount(); // current row count
tableWidget->setRowCount(row+1); // add one row
I am able to clear the whole QTableWidget but how i can delete all the rows created through setRowCount(row+1).


Thanks and Regards

derrickbj
13th October 2006, 15:40
You can always use QTableView::setColumnWidth() (http://doc.trolltech.com/4.2/qtableview.html#setColumnWidth) and call this both when your widget class is initialized and/or after your data loads.

For removing the rows, tableWidget->removeRow(row+1) ??