[Qt-interest] How to get the column of a certain HeaderItem from QTableWidget?

Pedro Santos nonspam2048 at yahoo.de
Wed May 12 15:33:48 CEST 2010


Hi,
I need to find the position of a certain HeaderItem of QTableWidget in order to 
be able to change the position of the table header and insert new items at the 
right position anyway.

I wrote the following in the constructor of my derived QTableWidget:

     _headerArticle = new QTableWidgetItem(tr("Article"));
     _headerTitle = new QTableWidgetItem(tr("Title"));
     [...] //some more code

     setHorizontalHeaderItem(0,_headerArticle);
     setHorizontalHeaderItem(1,_headerTitle);
         [...] //some more code

     horizontalHeader()->setMovable(true);

Then I have to insert items in the right column. I tryed to get the current 
column of the item in the following ways, but the position is every time 
returned as -1 which means the item isn't in the table but it is there as
the headers are shown correctly:


     QString title =  article->getTitle();
     QString condition =  article->getCondition();
         [...]

     QTableWidgetItem* itemTitle = new QTableWidgetItem(title);
     QTableWidgetItem* itemCondition = new QTableWidgetItem(condition);

     int row = rowCount();
     insertRow(row);

Both methods return -1:

     setItem(row, column(_headerTitle), itemTitle); // returns -1
     setItem(row, _headerCondition->column(), itemCondition); // returns -1

So whats wrong with that code and how can I get the column number of my HeaderItem?

Regards
Pedro Santos



More information about the Qt-interest-old mailing list