Results 1 to 3 of 3

Thread: QTreeWidget column size problem

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QTreeWidget column size problem

    Hello!

    This is my first time using a tree widget. I managed to put it together using QTreeWidget and QTreeWidgetItem. In the first column of every item I have a checkbox and in the second colum there is a QString. I want the string to be flushed left to the checkbox on every level, but it doesn't work. If I open the tree deep enough, upper levels resize and create a gap between the checkbox and the text. The attached picture illustrates the problem.

    treeview.png

    Here is how I create it:

    Qt Code:
    1. QTreeWidget *treeWidget = new QTreeWidget(this);
    2. treeWidget->setColumnCount(2);
    3. //treeWidget->header()->hide();
    4. treeWidget->header()->setResizeMode(0, QHeaderView::ResizeToContents);
    5. treeWidget->setAnimated(true);
    6. treeWidget->setSelectionMode(QTreeWidget::NoSelection);
    7. treeWidget->setFocusPolicy(Qt::NoFocus);
    8. QList<QTreeWidgetItem*> items;
    9. for (int i = 0; i < 3; ++i)
    10. {
    11. ti->setText(0, QString("item: %1").arg(i));
    12.  
    13. for (int j = 0; j < 3; ++j)
    14. {
    15. ti2->setCheckState(0, Qt::Unchecked);
    16. ti2->setText(1, QString("item: %1").arg(j));
    17.  
    18. for (int k = 0; k < 3; ++k)
    19. {
    20. ti3->setCheckState(0, Qt::Unchecked);
    21. ti3->setText(1, QString("item: %1").arg(k));
    22. ti2->addChild(ti3);
    23. }
    24.  
    25. ti->addChild(ti2);
    26. }
    27.  
    28. items.append(ti);
    29. }
    30. treeWidget->insertTopLevelItems(0, items);
    To copy to clipboard, switch view to plain text mode 


    How can I fix this?

    And while we are at it, I would prefer to have this Windows Explorer style with the plus button, as can be seen in the simple tree model example. How can I get that?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTreeWidget column size problem

    Set the checkState and the text on the same column:
    Qt Code:
    1. ti2->setCheckState(0, Qt::Unchecked);
    2. ti2->setText(0, QString("item: %1").arg(j));
    To copy to clipboard, switch view to plain text mode 

    The plus button is a function of the style. QCleanlooksStyle has a plus button.

  3. The following user says thank you to norobro for this useful post:

    Cruz (9th November 2012)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    383
    Thanks
    99
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget column size problem

    Thanks mate! Both suggestions work perfectly.

    I'm going to need a second widget just like this, but instead of the checkboxes I will have to use sliders. Before I get lost in the depths of the model view framework, can anyone please point out the clear path to enlightment?

Similar Threads

  1. QTreewidget stylesheet problem: second column
    By trallallero in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2011, 12:11
  2. QTreeWidget Column Reordering
    By jimc1200 in forum Newbie
    Replies: 1
    Last Post: 8th January 2009, 08:51
  3. QTreeWidget - How to get a column text ?
    By fabriciols in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2008, 16:32
  4. QTreeWidget scroll only one column
    By nina1983 in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2008, 15:07
  5. Sorting column in QTreeWidget
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2007, 18:35

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.