[Solved] Adding text to cells of a QTableWidget by code?
-
Hi CAD_coding,
you can add any kind of widgets to your cells with the following:
yourTableWidget->setCellWidget(cellWidget, row, col);e.g. Create a QLabel and pass it to setCellWidget().
You can also use QTableWidgetItem
there you can use the function setText().
with yourtableWidget->setItem(row, col, yourItem) you can add it to the QTableWidget. -
Hi,
Update the "item":http://qt-project.org/doc/qt-4.8/qtablewidget.html#item of the cell
-
So it's working like you wanted ?
If that's the case don't forget to update the thread's title to solved so other forum users may know that a solution has been found.
-
I am a bit confused here!
Do I have to create a QTableWidgetItem then add text to it & then setItem?
or directly call setText on QTableWidgetItem with correspondin row & column? -
If you don't have an item already, create a new one with the text you want and set it.
If the item already exists update it's text. -
Hi SGaist,
I had to create a QTableWidgetItem in heap memory & set it as an item in my table widget.
Now I have some questions:- Is the item automatically deleted when the table is deleted?
According to "this":http://qt-project.org/doc/qt-4.8/qtablewidget.html#setItem :
bq. The table takes ownership of the item.
So I think it should be deleting the items while deleting the table. Correct?
- I am having some problem in setting the alignment of text.
According to "this":http://qt-project.org/doc/qt-4.8/qtablewidgetitem.html#setTextAlignment it takes only 1 int arg. Now I want HCENTRE & VCENTRE Alignment both. I tried by passing (Qt::AlignHCentre && Qt::AlignVCentre). It doesnt work.
- Is the item automatically deleted when the table is deleted?
-
-
Correct
-
Sure it doesn't work, your are doing a logical AND, that's not the way to set flags you should either use Qt::AlignCenter or Qt::AlignHCenter | Qt::AlignVCenter, have a look at the "Alignment enum":http://qt-project.org/doc/qt-4.8/qt.html#AlignmentFlag-enum
-
-
ohh got it!
Thanks