Results 1 to 9 of 9

Thread: How to word wrap text in the rows and columns of a QTableWidget?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to word wrap text in the rows and columns of a QTableWidget?

    I tried:

    Qt Code:
    1. QTableWidget *j = new QTableWidget (10000, 5, centralWidget);
    2. j->setColumnWidth (0, 500);
    3. j->setColumnWidth (1, 30);
    4. j->setColumnWidth (2, 30);
    5. j->setColumnWidth (3, 320);
    6. j->setColumnWidth (4, 310);
    7.  
    8. j->setWordWrap (true);
    To copy to clipboard, switch view to plain text mode 
    Also tried resizeColumnsToContents and resizeRowsToContents, but failed.

    If the text is longer than the set width, I want the sentence to get break down.
    Currenty, the lengthy part of the sentence just doesn't get shown.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    Please show how you populate an item in to the table.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    Qt Code:
    1. QTableWidget *j = new QTableWidget (1000, 5, centralWidget);
    2. j->setColumnWidth (0, 450);
    3. j->setColumnWidth (1, 50);
    4. j->setColumnWidth (2, 55);
    5. j->setColumnWidth (3, 320);
    6. j->setColumnWidth (4, 310);
    7. j->setHorizontalHeaderLabels (QString ("Sub-tasks;Time alloted;% complete;What's pending?;Reasons for the lag").split(";"));
    8.  
    9. QComboBox *p = new QComboBox [1000];
    10. for (int i = 0; i < 1000; i++)
    11. {
    12. p[i].addItem ("20");
    13. p[i].addItem ("30");
    14. p[i].addItem ("40");
    15. }
    16. for (int i = 0; i < 1000; i++)
    17. j->setCellWidget (i, 1, &p[i]);
    18.  
    19.  
    20. QComboBox *p1 = new QComboBox [1000];
    21. for (int i = 0; i < 1000; i++)
    22. {
    23. p1[i].addItem ("0");
    24. p1[i].addItem ("10");
    25. p1[i].addItem ("20");
    26. p1[i].addItem ("30");
    27. p1[i].addItem ("40");
    28. p1[i].addItem ("50");
    29. p1[i].addItem ("60");
    30. p1[i].addItem ("70");
    31. p1[i].addItem ("80");
    32. p1[i].addItem ("90");
    33. p1[i].addItem ("100");
    34. }
    35. for (int i = 0; i < 1000; i++)
    36. j->setCellWidget (i, 2, &p1[i]);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    WTF!
    When you will have 100000 items will you create 200000 QComboBoxes? See QItemDelegate and QAbstractItemView::setItemDelegate.

    @topic: I would try QTableWidgetItem::setTextAlignment with Qt::TextWordWrap (merge it with alignment flags if you need).
    I know that documentation doesn't say anything about that, its only about text alignment, but I'm quite convince that it will work.
    Remember also that you need enough cell hight for text warping so I expect in code some change in line hight.
    Last edited by MarekR22; 5th March 2012 at 12:37.

  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: How to word wrap text in the rows and columns of a QTableWidget?

    ItemViews don't word-wrap elements by themselves. If you really want that, implement a custom delegate. It will probably kill your CPU with large amounts of items that change their contents but what the heck... it's doable. Not that those comboboxes have anything to do with it... They will kill your CPU on their own once you start scrolling with or without word-wrapping.
    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.


  6. #6
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    This is strange. It works without ANY line of code!
    I just created simple project and it wraps text out of the box.
    I mean I didn't wrote a single line of code I just clicked couple times in designer and filled table with data (I didn't change ANY property).

    See this project tableTextWrapTest.zip when you manually extend line text is nicely warped. Tested on Linux Qt 4.8.

    Maybe you should specify more precisely what is your problem.
    Last edited by MarekR22; 5th March 2012 at 13:37.

  7. #7
    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: How to word wrap text in the rows and columns of a QTableWidget?

    Maybe it depends on the version of Qt? Seems to wrap on my 4.7 as well.
    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.


  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    I think none of the above has to do with the problem of OP.
    She was complaining about word wrapping in the view - while populating combo boxes.
    Based on the documentation, pure text items should indeed be wrapped by the view its self.

    @OP:
    Where the text does not get wrapped?
    In the combo boxes - or do you have "naked" text items in your table which do not word wrap?
    Try populating only strings in to the table - with no combo boxes.
    Does the text get wrapped then?

    In order to reduce non related other problems as the posters above me mentioned, try working with a small table, 5x5 for example.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to word wrap text in the rows and columns of a QTableWidget?

    She filled combo boxes with short text (the longest is "100"), so it is not about text wrapping in combo boxes.
    More probable is that she expects that line hight is growing when text is long and require warping ergo extra text lines.
    I did something like that (not in the table view) and it is quite tricky.
    Lets wait what author of this tread will say, our guessing leads nowhere.

Similar Threads

  1. word wrap
    By deeee in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2010, 19:55
  2. Long text and word wrap in QPainter
    By TomASS in forum Qt Programming
    Replies: 2
    Last Post: 11th December 2009, 11:50
  3. QTableWidget: Disable 'snapping' to rows and columns?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2008, 20:04
  4. Word wrap in QListView
    By jiveaxe in forum Qt Programming
    Replies: 33
    Last Post: 1st September 2007, 21:06
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.