Results 1 to 11 of 11

Thread: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

  1. #1
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Angry Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Hey guys.
    I need a scroll view which can add children one by one from top to bottom. and it can be auto-resized. and also the children labels will hold the whole view space. Here is the code.
    Qt Code:
    1. class QScrollView : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. QScrollView( QWidget* parent = 0 )
    7. {
    8. QHBoxLayout* layout = new QHBoxLayout(this);
    9. setLayout( layout );
    10.  
    11. scrollArea = new QScrollArea(this);
    12. layout->addWidget( scrollArea );
    13.  
    14. view = new QWidget(scrollArea);
    15. viewLayout = new QVBoxLayout(view);
    16. viewLayout->setAlignment( Qt::AlignTop );
    17. view->setLayout(viewLayout);
    18.  
    19. scrollArea->setWidget( view );
    20. scrollArea->setWidgetResizable(true);
    21. scrollArea->verticalScrollBar()->setSingleStep( 20 );
    22.  
    23.  
    24. QLabel* label = NULL;
    25. for (int i=0; i<1; ++i)
    26. {
    27. label = new QLabel("<font color='#000000' size='2'>Abc defg hi..END_OF_LABEL</font>", view );
    28. label->setWordWrap(true);
    29. label->setAutoFillBackground( true );
    30.  
    31. // QSizePolicy sp = label->sizePolicy();
    32. // sp.setVerticalPolicy(QSizePolicy::Expanding);
    33. // label->setSizePolicy(sp);
    34.  
    35. QPalette p = label->palette();
    36. p.setColor( QPalette::Window, QColor(255,0,255) );
    37. label->setPalette(p);
    38.  
    39. viewLayout->addWidget( label );
    40. }
    41. }
    42.  
    43. protected:
    44. QScrollArea* scrollArea;
    45. QWidget* view;
    46. QVBoxLayout* viewLayout;
    47.  
    48. };
    To copy to clipboard, switch view to plain text mode 

    it works well when the label has just a few characters.

    But when the label has a larger number of characters, i'm in a mess.
    To show the label's text, i must set it's Word Wrap property, right? but this will cause a problem, when i narrow the main window, the label don't show all the text. it has been cut off! finally I found that I can set the label's size policy by using "Expanding" or 'MinimumExpanding', it will solve the display problem when narrowing. but on the other side, the label dont has the fit height when i resize the window to a larger width scale. After trying all the parameters, i can't still find the answer. seeking for anyone's help now. I will greatly appreciate that!!!!
    Last edited by HiJack; 1st June 2010 at 12:03.

  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: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    I am not quite sure I follow.
    The idea of a scroll area is that what ever is in the scroll area is not being resized, rather, that the view or window is resized, and the content does not.
    This is why you get scroll bars to navigate in the content if the content is larger then the window.
    So I am not quite sure what it is you are trying to do with the resizing of the content..
    ==========================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 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    I just want to show the label ( with a large number of characters) and expand the content correctly.



    * The content can be expanded just vertically, it has the same width as the main window ( in another words, not allowed to be larger than the width of the main window) when resizing the window, the horizontal slider should be disabled.

    * With labels word wrap, when showing lots of characters (e.g. I insert a article to the label )the content should grow vertically, and the label always show all the text.(this will not happen when setting 'QSizePolicy::Minimum', it will lost some characters.)

    * All the labels can be shrunk to a fit height vertically, and aligned with the top. ( this wil not happen when setting the label with 'QSizePolicy::Expanding' or 'QSizePolicy::MinimumExpanding' . Do I must use 'viewLayout->setAlignment( Qt::AlignTop );'? )





    If I just put a few characters into the label, everything is fine. But it became worse when I put more characters into it. what I have missed?

  4. #4
    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: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Ok.
    So if I understand correctly your problem is:
    the label dont has the fit height when i resize the window to a larger width scale.
    Translated:
    Everything works, except, that when you resize the window to have a larger width, the labels will remain narrow, is that correct?

    If so, try adding a vertical spacer in the layout that holds the labels, it will "squeeze" the labels to take as much horizontal space as they can.
    ==========================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.

  5. #5
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Quote Originally Posted by high_flyer View Post
    Ok.
    So if I understand correctly your problem is:

    Translated:
    Everything works, except, that when you resize the window to have a larger width, the labels will remain narrow, is that correct?

    If so, try adding a vertical spacer in the layout that holds the labels, it will "squeeze" the labels to take as much horizontal space as they can.
    Nope. buddy.. in fact my problem is the vertical space. would u please create a rapid demo project in a few seconds? and you will understand it right now. Keep narrowing the windows. it will lost some characters. my test OS is windows XP.
    Qt Code:
    1. /////////////////////////////////////////////scrollview.h//////////////////////////////////////////////////////////////
    2. #ifndef ScrollView_H
    3. #define ScrollView_H
    4.  
    5. #include <QtGui>
    6.  
    7. class QScrollView : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. QScrollView( QWidget* parent = 0 )
    13. {
    14. QHBoxLayout* layout = new QHBoxLayout(this);
    15. setLayout( layout );
    16.  
    17. scrollArea = new QScrollArea(this);
    18. layout->addWidget( scrollArea );
    19.  
    20. view = new QWidget(scrollArea);
    21. viewLayout = new QVBoxLayout(view);
    22. viewLayout->setAlignment( Qt::AlignTop );
    23. view->setLayout(viewLayout);
    24.  
    25. scrollArea->setWidget( view );
    26. scrollArea->setWidgetResizable(true);
    27. scrollArea->verticalScrollBar()->setSingleStep( 20 );
    28.  
    29.  
    30. QLabel* label = NULL;
    31. for (int i=0; i<1; ++i)
    32. {
    33. label = new QLabel("<font color='#000000' size='2'>Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi.........END_OF_LABEL</font>", view );
    34. label->setWordWrap(true);
    35. label->setAutoFillBackground( true );
    36.  
    37. // QSizePolicy sp = label->sizePolicy();
    38. // sp.setVerticalPolicy(QSizePolicy::Expanding);
    39. // label->setSizePolicy(sp);
    40.  
    41. QPalette p = label->palette();
    42. p.setColor( QPalette::Window, QColor(255,0,255) );
    43. label->setPalette(p);
    44.  
    45. viewLayout->addWidget( label );
    46. }
    47. }
    48.  
    49. protected:
    50. QScrollArea* scrollArea;
    51. QWidget* view;
    52. QVBoxLayout* viewLayout;
    53.  
    54. };
    55.  
    56. #endif // ScrollView_H
    57.  
    58. //////////////////////////////////////main.cpp/////////////////////////////////////////////////////////////////
    59. #include "scrollview.h"
    60. int main(int argc, char *argv[])
    61. {
    62. QApplication a(argc, argv);
    63. QScrollView w;
    64. w.show();
    65. return a.exec();
    66. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    I ran you code and I still don't understand the problem.
    When resizing the scroll view the labels resize to match the width as it should, no chars are lost...
    When narrowing down the scroll view the Labels get narrow, but increase their height to accommodate the long string.

    Maybe you can post a screenshot of the problem.
    ==========================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.

  7. #7
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Sure thing


  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: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Ah... you hit the minimum width of the label, so it got scrolled out (as you can see the horizontal scroll bar that appeared).
    Just add:
    Qt Code:
    1. label->setMinimumWidth(1);
    To copy to clipboard, switch view to plain text mode 
    at line 44 in your above code.
    ==========================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. The following 2 users say thank you to high_flyer for this useful post:

    HiJack (9th June 2010), iceheaven31 (13th May 2011)

  10. #9
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Quote Originally Posted by high_flyer View Post
    Ah... you hit the minimum width of the label, so it got scrolled out (as you can see the horizontal scroll bar that appeared).
    Just add:
    Qt Code:
    1. label->setMinimumWidth(1);
    To copy to clipboard, switch view to plain text mode 
    at line 44 in your above code.
    yeah that's it. it works. thank u very much.

    but I found there's an irrelevant problem here. If I set the 'word wrap' property, and simultaneity there's a word which has a larger width than the label, such as 'hi..END_OF_LABEL', it will be cut off.

    how can i deal with it? Can it be wrapped ? or display some'...'? ( Under the circumstances that the view has a fixed width.)

  11. #10
    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: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    I am not aware of any ready made option you can use, as far as I know, you will have to subclass QLabel and re implement how word wrapping works. (to wrap at any char and not only at end of words).
    ==========================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.

  12. #11
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment

    Quote Originally Posted by high_flyer View Post
    I am not aware of any ready made option you can use, as far as I know, you will have to subclass QLabel and re implement how word wrapping works. (to wrap at any char and not only at end of words).
    OK! You helped me a lot.

Similar Threads

  1. word wrap
    By deeee in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2010, 19:55
  2. KTitleWidget & word wrap
    By miraks in forum KDE Forum
    Replies: 3
    Last Post: 29th March 2009, 23:24
  3. Word wrap of QLabel
    By jimfan in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2008, 04:49
  4. Qt layouting + QLabel's word wrap = bug?
    By dimuz in forum Qt Programming
    Replies: 6
    Last Post: 5th September 2007, 11:15
  5. Word wrap in QListView
    By jiveaxe in forum Qt Programming
    Replies: 33
    Last Post: 1st September 2007, 21:06

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.