Results 1 to 1 of 1

Thread: [SOLVED] QTableWidget subclass and horizontal header

  1. #1
    Join Date
    Oct 2010
    Location
    Opole, PL
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question [SOLVED] QTableWidget subclass and horizontal header

    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:

    Qt Code:
    1. void WatchListWidget::insertRow(const Series &series)
    2. {
    3. int row = rowCount();
    4. QTableWidget::insertRow(row);
    5.  
    6. setItem(row, Series::Title, new QTableWidgetItem(series.title()));
    7. [...]
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. setColumnCount(0);
    2. insertColumn(0);
    3. setHorizontalHeaderItem(0, new QTableWidgetItem("Text", QTableWidgetItem::UserType));
    4. [...] // for all columns
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by nephre; 5th November 2010 at 23:13. Reason: Found solution/what was done wrong.
    In the 1960's people took LSD to make the world weird.
    Now the world is weird and people take Prozac to make it normal.

Similar Threads

  1. How to edit Horizontal Header Item in QTableWidget
    By ioannis in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2013, 18:50
  2. Replies: 1
    Last Post: 11th September 2009, 17:03
  3. Setting two horizontal header labels for QTablewidget
    By arunvv in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2009, 17:58
  4. How to customize horizontal header (diagonal header view)
    By vairamuthu.g in forum Qt Programming
    Replies: 4
    Last Post: 4th September 2008, 15:59
  5. QTable Horizontal Header Off
    By Solarity in forum Newbie
    Replies: 1
    Last Post: 17th March 2006, 20:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.