Results 1 to 4 of 4

Thread: WordWrap policy for QLabel

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default WordWrap policy for QLabel

    I need to set the wordwrap policy of Qlabel so that it wraps the text anywhere and not just the word-breaks. I have read that the same can be done in QTextEdit. Is this thing possible with QLabel?

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

    Default Re: WordWrap policy for QLabel

    I have a feeling that it might be easier to write a custom label widget with the help of QTextLayout than to hack QLabel for this.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: WordWrap policy for QLabel

    I am again back on the same problem..
    Somehow I am not able to figure out how to use the QTextLayout along with QLabel so that the text on the label is displayed according to the set WordWrap policy..
    thanks in advance for any kind of help

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

    Default Re: WordWrap policy for QLabel

    Perhaps you could simply use QTextEdit?
    Qt Code:
    1. class TextEdit : public QTextEdit
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. TextEdit(const QString& text, QWidget* parent = 0)
    7. : QTextEdit(text, parent)
    8. {
    9. // make it look and feel like an ordinary label
    10. setReadOnly(true);
    11. setFrameStyle(QFrame::NoFrame);
    12. QPalette pal = palette();
    13. pal.setColor(QPalette::Base, Qt::transparent);
    14. setPalette(pal);
    15.  
    16. // wrap anywhere, adjust minimum height on the fly
    17. setLineWrapMode(QTextEdit::WidgetWidth);
    18. setWordWrapMode(QTextOption::WrapAnywhere);
    19. connect(document()->documentLayout(),
    20. SIGNAL(documentSizeChanged(QSizeF)),
    21. this, SLOT(adjustMinimumSize(QSizeF)));
    22. }
    23.  
    24. private slots:
    25. void adjustMinimumSize(const QSizeF& size)
    26. {
    27. setMinimumHeight(size.height() + 2 * frameWidth());
    28. }
    29. };
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    rawfool (16th July 2013)

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.