Results 1 to 6 of 6

Thread: Squeeze text in QLabel

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Squeeze text in QLabel

    Hi,
    I have a dialog with a fixed width and a QLabel in it which will display text from a database; but the lines of text that will be displayed are some smaller and some larger of the gui. I don't want enable multiline in qlabel. Is it possible squeezing ( = scale horizontally) the text and fit so the gui width? I have attached an example of what i mean (i made it with qpainter and scale() ): the first row is exceeding the dialog width while the second one is the same line (with same font size) but squeezed.

    Thanks
    Attached Images Attached Images
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Squeeze text in QLabel

    Apart from playing with font styles and stretch factors( see QFont::setStretch) you could do it like you proposed: subclass QLabel and paint the text manually, using scale, etc.

    Regards

  3. #3
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Squeeze text in QLabel

    if you want to scale it or reset the pointsize of the font, you could calc with the length of the string (the string you want to draw onto the label) and the current pointsize of the font, wether its longer than the widgets width. if this is so, you reduce the pointsize of the font, by the factor you can calc with widget.width/(font.pointSize*string.length).

    hope you understand, bad english

  4. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Squeeze text in QLabel

    Thank marcel, but the problem is that text in database contains, some time, html tags and if i want render it using this method i have to use QTextDocument and, if possible, I would avoid it. Instead if was possible stretching the content of a qlabel it was perfect.

    Cheers
    Giuseppe CalÃ

  5. #5
    Join Date
    Jan 2007
    Posts
    177
    Thanks
    8
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Squeeze text in QLabel

    hä?
    when i understand you in the right way, you want to stretch the contents completely and not only some stringsa?
    than draw on a pixmap and set the pixmap to the label!

  6. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Squeeze text in QLabel

    ok kernel_panic, I give it a try.

    Regards
    Giuseppe CalÃ

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  3. QLabel, large, rich text, font size
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2007, 11:56
  4. Qlabel with (icon and text) HowTo ?
    By QiT in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2006, 08:14
  5. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 07:25

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.