Results 1 to 12 of 12

Thread: Set header labels

  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Set header labels

    I have created a QTreeWidget with 4 columns using a Designer. It works but my header columns are "1 2 3 4". I tried setHeaderLabels():

    Qt Code:
    1. Tree::Tree( QWidget *parent ) : QTreeWidget(parent)
    2. {
    3. QStringList labels = {"title", "author", "source", "date" };
    4.  
    5. setHeaderLabels(labels);
    6.  
    7. TreeHeader = header(); // TreeHeader is QHeaderView *
    8.  
    9. TreeHeader->setResizeMode(0,QHeaderView::Stretch);
    10. TreeHeader->setSectionHidden(1,true);
    11. TreeHeader->setSectionHidden(2,true);
    12. TreeHeader->setSectionHidden(3,true);
    13. TreeHeader->setVisible(false);
    14. }
    To copy to clipboard, switch view to plain text mode 

    (At the beginning: we have no header and only one column in the view.) but I still have columns "1 2 3 4". Perhaps, because setHeaderLabels() adds columns instead of updating them according to the documentation. I have searched something what updates header labels but I have found nothing. How should I set the header labels?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    setHeaderLabels() does not add new columns. However with QTreeWidget you should set headers by accessing QTreeWidget::headerItem().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set header labels

    GP fault. I updated the code:

    Qt Code:
    1. Tree::Tree( QWidget *parent ) : QTreeWidget(parent)
    2. {
    3. QTreeWidgetItem *HeaderItem = headerItem();
    4.  
    5. TreeHeader = header();
    6.  
    7. HeaderItem->setText(0,"title");
    8. HeaderItem->setText(1,"author");
    9. HeaderItem->setText(2,"source");
    10. HeaderItem->setText(3,"date");
    11.  
    12. setHeaderItem(HeaderItem);
    13.  
    14. TreeHeader->setResizeMode(0,QHeaderView::Stretch);
    15. TreeHeader->setSectionHidden(1,true);
    16. TreeHeader->setSectionHidden(2,true);
    17. TreeHeader->setSectionHidden(3,true);
    18. TreeHeader->setVisible(false);
    19. }
    To copy to clipboard, switch view to plain text mode 

    And got a GP fault when I tried to show the header after changing view:

    Qt Code:
    1. case 1 :
    2. {
    3. ui.splitter->setOrientation(Qt::Vertical); // wndTree is a QTreeWidget, wndText is a QTextEdit (descendants) in a splitter window
    4. ui.wndTree->TreeHeader->setSectionHidden(1,false);
    5. ui.wndTree->TreeHeader->setSectionHidden(2,false);
    6. ui.wndTree->TreeHeader->setSectionHidden(3,false);
    7. ui.wndTree->TreeHeader->setVisible(true); // <-- GP fault
    8. conf.ViewType = 2;
    9. }
    10. break;
    To copy to clipboard, switch view to plain text mode 

    The debugger shows that the HeaderItem in the Tree ctor is non null. Note: with "1 2 3 4" the code works as it should. Any suggestions?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    What is this TreeHeader member of ui.wndTree? How is it related to the local TreeHeader variable you create in constructor of the Tree class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set header labels

    The ui.wndTree is an instance of Tree. I have created a splitter layout in the Designer, put a QTreeWidget and a QTextEdit into it and promoted the QTreeWidget to Tree and QTextEdit to Text. The Tree class contains a TreeHeader pointer to QHeaderView, which should be shown in some views and not shown in others. The views are:

    case 1: side by side, no header
    case 2: top and bottom, header
    case 3: only tree, header
    case 4: only text, no header

    The views are cycled by a view selector.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    In the constructor of Tree you are not initializing the TreeHeader member variable but rather you create a new local variable that shadows the member.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set header labels

    No, I am not. header() returns the pointer to the tree header which I store in the variable TreeHeader. It is the pointer to the actual header because my updating (expand first column, hide next three ones) is reflected by the header. The problem consists in my bad understanding headerItem().

    According to the documentation, on setHeaderItem(), the header takes ownership of the supplied pointer and it (should) use texts in the supplied QTreeWidgetItem for the header columns. But what happens to the original headerItem()? A possible scenario: the original QTreeWidgetItem is deleted and the supplied one is set. Because I supply the pointer returned by headerItem(), an invalid pointer is set and a GP fault follows on the first use of the pointer. The pointer is first used on setVisible().
    e
    Well, but what to do then?

    (1) Do not call setHeaderItem(): columns "1 2 3 4", the app will not crash.
    (2) Create a new QTreeWidgetItem instead of updating headerItem(), set texts, call setHeaderItem(): columns "1 2 3 4", the app will not crash. In the other words, setting texts does not suffice.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    Quote Originally Posted by Radek View Post
    Well, but what to do then?
    Your basic problem is that you call setHeaderItem() with the already set header item. You don't have to do that, just modify the item itself -- it is not a copy but the actual header item that you are modifying.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. int main(int argc, char **argv) {
    4. QApplication app(argc, argv);
    5. w.headerItem()->setText(0, "col0");
    6. w.headerItem()->setText(1, "col1");
    7. w.show();
    8. w.addTopLevelItem(new QTreeWidgetItem());
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 23rd August 2013 at 09:20.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set header labels

    This is "method 1" from the above. It does not work:

    Qt Code:
    1. Tree::Tree( QWidget *parent ) : QTreeWidget(parent)
    2. {
    3. TreeHeader = header();
    4.  
    5. headerItem()->setText(0,"title");
    6. headerItem()->setText(1,"author");
    7. headerItem()->setText(2,"source");
    8. headerItem()->setText(3,"date");
    9.  
    10. TreeHeader->setResizeMode(0,QHeaderView::Stretch);
    11. TreeHeader->setSectionHidden(1,true);
    12. TreeHeader->setSectionHidden(2,true);
    13. TreeHeader->setSectionHidden(3,true);
    14. TreeHeader->setVisible(false);
    15. }
    To copy to clipboard, switch view to plain text mode 

    columns "1 2 3 4".

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    Apparently you are doing something later on which modifies the labels. The code above hides the header whatsoever so if you see the header then somewhere else you have some code that shows it. Run my example and see that it works. The problem is elsewhere in your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Set header labels

    No success, wysota, at least, no success in Debian KDE. I commented out everything concerning the tree header from everywhere and left only headerItem()->setText() statements in the ctor. Then I made the header visible in the .ui file. Try. Columns "1 2 3 4". But I found out how to set the header texts in the Designer: Make the header visible, double click it and select "edit items". Overwrite "1 2 3 4", you can hide the header again. Recompile, try. It works

    In the .ui file, I got the sections in the wndTree description:
    Qt Code:
    1. <column>
    2. <property name="text">
    3. <string notr="true">title</string>
    4. </property>
    5. </column>
    To copy to clipboard, switch view to plain text mode 

    for each header string. A little surprise is, that there is nowhere specified which column. This seems to be done only by position in the .ui file.

    Problem solved for now but a question remains open: How to set column headers on fly? For example, from some configuration file?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Set header labels

    Quote Originally Posted by Radek View Post
    No success, wysota, at least, no success in Debian KDE.
    Did you run my example? Did it fail?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. show User Defined Header Labels in QTableView
    By nagabathula in forum Qt Programming
    Replies: 3
    Last Post: 11th August 2011, 06:38
  2. Cannot see the header labels SQL
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2011, 01:38
  3. QTableWidget custom header labels
    By migel in forum Newbie
    Replies: 1
    Last Post: 1st June 2011, 09:20
  4. Setting two horizontal header labels for QTablewidget
    By arunvv in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2009, 17:58
  5. Replies: 1
    Last Post: 23rd August 2006, 18:02

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
  •  
Qt is a trademark of The Qt Company.