Results 1 to 12 of 12

Thread: QTreeWidget's Item Widgets

  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QTreeWidget's Item Widgets

    I might have found a bug in Qt 4.1.

    QTreeWidget::setItemWidget(), which was introduced in Qt 4.1 allows a widget to be displayed in an tree item's cell.

    An example code below creates a simple QTreeWidget with 10 rows and 2 columns.
    The first column contains item's index as text and the second column contains a QLabel with same text, respectively.

    Qt Code:
    1. tree->setColumnCount(2);
    2.  
    3. for (int i = 0; i < 10; ++i) {
    4. item->setText(0, QString::number(i));
    5. tree->addTopLevelItem(item);
    6. tree->setItemWidget(item, 1, new QLabel(QString::number(i)));
    7. }
    To copy to clipboard, switch view to plain text mode 

    Resizing the QTreeWidget small enough to get the vertical scrollbar visible and then scrolling back and forth messes up the item widget drawing in the second column. Though, resize event seems to fix the erroneous drawing.




    - JP

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

    Default Re: QTreeWidget's Item Widgets

    You forgot to add the second image.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget's Item Widgets

    Second image fixed.

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

    Default Re: QTreeWidget's Item Widgets

    So what is wrong with that widget? It looks fine...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget's Item Widgets

    Quote Originally Posted by wysota
    So what is wrong with that widget? It looks fine...
    Look at the code --- numbers in both columns should be the same.

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

    Default Re: QTreeWidget's Item Widgets

    They are not the same even in the first picture...

    I thought the display was being cluttered, but it looks like the issue is different

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget's Item Widgets

    1) Here it is shown correctly:



    2) After resizing and then scrolling back and forth it becomes messed:



    3) Resizing again fixes the drawing:



    Above screenshots are taken in WinXP SP2.


    Same malfunction detected also in KDE 3.4.3:


  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidget's Item Widgets

    I've just checked on my system (PLD Linux, Qt 4.1.0) and it also doesn't work the way it should.

    Here is the full example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QTreeWidget>
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app( argc, argv );
    8.  
    9. QTreeWidget* tree = new QTreeWidget;
    10. tree->setColumnCount(2);
    11.  
    12. for (int i = 0; i < 10; ++i) {
    13. item->setText(0, QString::number(i));
    14. tree->addTopLevelItem(item);
    15. tree->setItemWidget(item, 1, new QLabel(QString::number(i)));
    16. }
    17.  
    18. tree->show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    @jpn: Did you try the latest snapshot?

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

    Default Re: QTreeWidget's Item Widgets

    This is my system (compiled Qt4.1), also not correct:
    Attached Images Attached Images
    • File Type: jpg x.jpg (7.7 KB, 22 views)

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget's Item Widgets

    Alright, I grabbed the snapshot from yesterday (4.1.1-20060209). The original bug seems to have already been fixed. Despite of scrolling, all item widgets are always drawn in correct positions. Keep up the great work, Trolltech!

    Nevertheless, another cosmetic issue related to this appeared. Selecting rows seems to mess up the item widget drawing too (especially QLabel, as in this case). The following image explains this another cosmetic bug(?):


    Rows 2,4,6 have each been selected before selecting row 8.

    (WinXP+SP2 / Qt-4.1.1-snapshot-20060209)

  11. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget's Item Widgets

    Just for your information..
    The reason for this problem is that QLabel has the autoFillBackground set to false by default.
    This means that you will see both the item and the label contents. Setting autoFillBackground to true fixes this problem.
    Task Tracker - Entry 103100
    J-P Nurmi

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeWidget's Item Widgets

    I'd claim this does not FIX the problem, this method only covers it.
    It really is interesting why does the QTreeView draw the contents of the label.

    The rejecting reason says "you will see both the item and the label contents"..
    But the items in the second column should be empty. There has been set text only
    for the items in the first column, and a label widget for the items in the second column!

    Besides, forcing the QLabel to fill it's background also naturally overrides the selection drawing.

    I managed to find something very interesting by accident:
    Qt Code:
    1. #include <QtGui>
    2. int main( int argc, char **argv )
    3. {
    4. QApplication app(argc,argv);
    5. QTreeWidget* tree = new QTreeWidget;
    6. tree->setColumnCount(1);
    7. for (int i = 0; i < 10; ++i)
    8. {
    9. QTreeWidgetItem* item = new QTreeWidgetItem(tree);
    10. QWidget* widget = new QWidget;
    11. tree->setItemWidget(item, 0, widget);
    12. }
    13. tree->show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    The above code is a dummy example showing an "empty" tree with widgets.
    The funny thing is, when you click items, there is an output like this:
    QWidget:roperty("text") failed: property invalid or does not exist
    QWidget::setProperty("text", value) failed: property invalid, read-only or does not exist
    So clearly the text-property of a widget is requested.
    I took a look at the Qt sources, and I found that it's QItemDelegate::setModelData() which requests this property.

    As for a test, I subclassed QItemDelegate and overrode setData() by an empty mplementation body.
    Voila! The QLabel gets drawn correctly in a QTreeWidget. Or more correctly the item does not draw the label contents. Quite an ugly workaround, that is..

    I guess I'll have to bug the Trolls, maybe there is a solution out there..
    J-P Nurmi

Similar Threads

  1. View, Scene, Item and thread??
    By dungsivn in forum Qt Programming
    Replies: 5
    Last Post: 20th August 2008, 19:21
  2. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  3. QTreeWidget, resizing item and widgets problem
    By The Storm in forum Qt Programming
    Replies: 15
    Last Post: 17th March 2008, 21:20
  4. Replies: 1
    Last Post: 19th April 2007, 22:23
  5. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28

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.