Results 1 to 9 of 9

Thread: [QT4] QTreeView and rows with multiple lines

  1. #1
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default [QT4] QTreeView and rows with multiple lines

    I've got a field in my TreeView that takes the text from a QTextEdit and stuffs it into a row in my tree. Unfortunately, if there's a newline, I get odd output in the field. It shows half of one line, and half of another - neither of which is really readable.

    Is there a way to cause the tree view to recognize the newlines and expand the row?

    I saw a property called "uniformRowHeights" in my tree view... it's currently set to "false", but I still see this behaviour.

    Any suggestions? Thanks.
    Life without passion is death in disguise

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

    Default Re: [QT4] QTreeView and rows with multiple lines

    You have to reimplement the delegate -- sizeHint() should return the size of the item needed to fit all the text (which is to be calculated using QFontMetrics) and paint() should do the painting (using QPainter::drawText() with word wrapping flag active).

  3. #3
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] QTreeView and rows with multiple lines

    Strange,

    QT 4.1.1 and QT 4.1.0 behaving differently here.
    When running the attached example I'm getting a different output depending on the library I'm using. With QT 4.1.0 (picture on the right) I'm getting what I would have expected whilst with QT 4.1.1 the second line is gone.

    I found that by chance and I'm wondering if that is supposed not to work anymore.

    Qt Code:
    1. MModel *model = new MModel;
    2. QTreeView *view = new QTreeView;
    3. view->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QVariant MModel::data(const QModelIndex &index, int role) const{
    2. if( role!=Qt::DisplayRole)
    3. return QVariant();
    4.  
    5. const MItem *item = indexToItem(index);
    6. int col = index.column();
    7. switch(col) {
    8. case 0: return item->name();
    9. // ============================
    10. case 1: return QString("Line1 \n Line2");
    11. //============================
    12. default: break;
    13. }
    14. return QVariant();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question Re: [QT4] QTreeView and rows with multiple lines

    Hmm... good example.

    So the question becomes, what is the intended result from the library? Should we have to go in and define a custom delegate with an adjusted size hint, or should Qt handle newlines? Should we wait for a bug fix or is this the intended behaviour?
    Life without passion is death in disguise

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

    Default Re: [QT4] QTreeView and rows with multiple lines

    There should be an option for both. But there isn't You can report a suggestion if you wish (and it it hasn't been done before).

  6. #6
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4] QTreeView and rows with multiple lines

    Well, after some searching, I was not able to find a closely related item in their bug list... so I went ahead and posted N107752. I got the following response:
    Quote Originally Posted by Sigrid Fjell Nævdal
    On Tuesday, 21. Mar 2006 19:28 KShots@warfaresdl.com wrote:

    Hello,

    The example below works fine for me in the latest snapshot. I suggest
    you try the latest snapshot to see if this solves your problem.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTreeView>
    3. #include <QStandardItemModel>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8. QTreeView view;
    9. // Prepend 4 rows into the model
    10. model->insertRows(0, 4);
    11. // Prepend 4 columns into the model
    12. model->insertColumns(0, 4);
    13.  
    14. for (int row = 0; row < 4; row++) {
    15. for (int col = 0; col < 4; col++) {
    16. // Return a model index for the given row and column.
    17. QModelIndex index = model->index(row, col);
    18. // Set the index's data to the specified value
    19. model->setData(index, QVariant("hello\nnextline"));
    20. }
    21. }
    22.  
    23.  
    24. view.setModel(model);
    25. view.show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    >
    > Created a model and linked it to a QTreeView. The data() in one of the
    > columns contains newline characters.
    >
    > What I expected to see:
    >
    > Some rows of the tree having multiple lines, depending on the number
    > of newlines / line wraps in the data
    >
    > What I got instead:
    >
    > Each row had exactly one line of text, with some rows showing some
    > portion of multiple lines of text (looks like it just looks at the
    > "middle"
    >
    > More info:
    >
    > This is discussed in some more depth (with some examples) at the
    > following URL:
    >
    > http://www.qtcentre.org/forum/showth...9&goto=newpost
    >
    > Note that it appears to be working in 4.1.0, and breaks again in 4.1.1
    > according to the topic.
    >

    Best regards,

    Sigrid Fjell Nævdal
    --
    Trolltech AS, Sandakerveien 116, P.O.Box 4332 Nydalen, 0402 Oslo, Norway
    ... so apparently, the latest snapshot works correctly. I guess that means that 4.1.2 or whatever the next official version is will have this fixed.
    Life without passion is death in disguise

  7. #7
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: [QT4] QTreeView and rows with multiple lines

    For the record, I have verified Moppel's observation with my own code - it performs as expected with 4.1.0, and goes back to one line in 4.1.1...

    However, in 4.1.1, instead of getting a snapshot of the middle of the box, I get the top of the box. I'd call that an improvement over 4.0.1, though I'm not sure why they took a step back from 4.1.0.
    Life without passion is death in disguise

  8. #8
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] QTreeView and rows with multiple lines

    Also for the record:

    I tried my example posted last week with a freshly compiled QT:
    qt-x11-opensource-src-4.1.3-snapshot-20060328

    And guess what: I works again. Hurray!
    Looks like we have to wait for the next QT release.

    Cheers,

    Moppel

  9. #9
    Join Date
    Jan 2006
    Posts
    33
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4] QTreeView and rows with multiple lines

    http://www.trolltech.com/developer/c...ges-4.1.2.html

    - QTreeWidget
    Fix multi line text items.

  10. The following user says thank you to Moppel for this useful post:

    razvan.petru (18th November 2009)

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.