Results 1 to 7 of 7

Thread: Need Graphical Text on QPushButton/QLabel

  1. #1
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Need Graphical Text on QPushButton/QLabel

    I'm wanting to create more graphical text to put on our QPushButtons and QLabels. I mean text with effects, like gradients...outlines...etc.

    How would you gurus recommend approaching something like this? I'd like to avoid pre-creating images for all the text I'll need, as we'll need to translate it later on. I know QPainter can achieve all the effects I need.

    Would the best method be to get the region of the button or label I want the graphical text on, then use QPainter to paint the text I need inside the area? That was my initial thought, but somehow I don't like that.

    Thanks for any help you can give, Daniel

    EDIT: Been reading up on QPainter - I notice it can take a QPaintDevice in its constructor as the object to paint to. Looking at QPaintDevice I see that QPixmap implements it. Would it be feasible (efficient) to paint my graphical text to a QPixmap object, then set the QPixmap as the button's icon? I'm going to experiment of course, just want to do it the best possible way.
    Last edited by JimDaniel; 3rd June 2008 at 04:46.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need Graphical Text on QPushButton/QLabel

    Use stylesheets
    You can then play with qlineargradient or other. Refer Qt Style sheets in Assistant

  3. #3
    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: Need Graphical Text on QPushButton/QLabel

    Quote Originally Posted by JimDaniel View Post
    I'm wanting to create more graphical text to put on our QPushButtons and QLabels. I mean text with effects, like gradients...outlines...etc.
    I think you'll have to subclass the button (or the label) and implement the functionality you want. You can use pixmaps as you suggested, but the result will probably be not what you want.

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Need Graphical Text on QPushButton/QLabel

    Quote Originally Posted by wysota View Post
    I think you'll have to subclass the button (or the label) and implement the functionality you want. You can use pixmaps as you suggested, but the result will probably be not what you want.
    Not only was it not what I wanted, but I couldn't get it to work that way at all.

    I ended up subclassing, and drawing my own text. I'm having more difficulty though.

    See, I want the button to be drawn as it normally is, so first I pass the paintEvent up to the parent QPushButton paintEvent. Then I draw my own graphical text. The problem is, I want to be able to set the text and icon the normal way, but if I do that, then the parent paintEvent draws text/icon first. What is the best way to stop the parent from drawing text/icon, and still keep the normal way of setting those properties? Does that make sense? I guess I can think of a work-around for it, like storing a temp variable somewhere, but I'm wondering if there is a cleaner way.

  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: Need Graphical Text on QPushButton/QLabel

    In general you can use QStylePainter that will do most of the work for you. You'll be passing a QStyleOption subclass to it and there you can control the text and pixmap that will be drawn. You can pass empty values there so that the style will draw everything apart the text and icon. Then it's just a matter of calculating the shift for pressed buttons, colours for disabled buttons and then you can draw your effects.

  6. #6
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Need Graphical Text on QPushButton/QLabel

    Great, that's just what I was looking for. Is there also a way to get at the positioning info for the text and icon? Or is that something I'm just going to have to calculate myself?

  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: Need Graphical Text on QPushButton/QLabel

    Calculate it yourself or check what QStyle has to offer.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Match the text beetween two string
    By dreamer in forum Qt Programming
    Replies: 4
    Last Post: 20th May 2008, 14:48
  4. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.