PDA

View Full Version : [SOLVED] QTableWidget subclass and horizontal header



nephre
5th November 2010, 23:41
Hello,

I have created a subclass of QTableWidget, but have problem with setting column titles.
GUI was created in QtDesigner with QTableWidget as base class. Three columns were created by simply doubleclicking table widget in QtDesigner and using "Modify table" editor. After compilation, table widget had 3 columns with provided names.

After that base QTableWidget class was replaced by descendant class using "Promoted widgets" feature. Everything seems to work fine, except column titles, previously defined in QTableWidget: in subclass they are sequential column numbers, starting from number 1.

I tried to use setHorizontalHeaderLabels() and setHorizontalHeaderItem(), with many combinations of parameters, like changing QTableWidgetItemType::Type to QTableWidgetItemType::UserType, setting column count before setting horizontal header item, after setting, and so on, but all that with no apparent result.

All those attempts to set custom table widget's title were executed inside class methods. What I find interesting (and strange) is that following piece of code works as expected, so obviously it is possible to change columns contents from inside class:



void WatchListWidget::insertRow(const Series &series)
{
int row = rowCount();
QTableWidget::insertRow(row);

setItem(row, Series::Title, new QTableWidgetItem(series.title()));
[...]


When I try to do the same, but with with header methods:



setColumnCount(0);
insertColumn(0);
setHorizontalHeaderItem(0, new QTableWidgetItem("Text", QTableWidgetItem::UserType));
[...] // for all columns


it works (column is added), but still there are only column numbers instead of "Text" as a title. Besides, I noticed it does not work at all when called within my class.

The only successful attempt was to set section titles outside class, calling public QTableWidget methods (or wrapper method, because I don't intend to change widget's column names or amount later).

Thus, I have some questions:

is calling public methods outside QTableWidget class the only way of setting header contents?
If no, what am I missing/doing wrong?
Or maybe it's just stupid idea to create item widgets inside table itself? :) (but works for regular table cells)


Thanks upfront for any replies.


[EDIT:]
How funny is that after few hours of searching for an answer, googling and digging through docs, finally it occurs, that the problem was somewhere else, but so close. And the solution comes a milisecond after posting question on forum.

In my code I used QTableWidget::clear(), which obviously clears widget contents and hides headers. I saw clear() in code completion, but didn't read complete documentation and that's my failure.
I didn't suspect it initially, because nowhere in my code re-creating headers row was required, while method calling clear() is called many times and headers are always there. Instead, it's enough to use clearContents().

Hope that helps anyone.