Results 1 to 4 of 4

Thread: QPushButton: Outline white text in black on transparent background

  1. #1
    Join Date
    May 2010
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default QPushButton: Outline white text in black on transparent background

    I have a QPushButton which displays white text on a transparent background.. but it isn't always obvious depending on what is behind it.

    I'm trying to outline the text in black 1px wide but am getting nowhere with stylesheets. Anyone know how to do this?

    (note: I'm not trying to outline the button with a black box.. I'm trying to outline the actual text.. )

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton: Outline white text in black on transparent background

    You cannot outline the text with a stylesheet, as the pushbutton relies on the drawText method. Instead, you will have to sub-class and implement your own button. In that button, you must create a QPainterPath from your text and outline it.

  3. The following user says thank you to e8johan for this useful post:

    Tito (15th September 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    28
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QPushButton: Outline white text in black on transparent background

    I'm trying to do the same for QPushButton, QMessageBox and QMenu. Did you come up with some working code, and can I look at it?

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPushButton: Outline white text in black on transparent background

    You need to use QPainterPath::addText() instead of QPainter::drawText(), here is sample working code, put this into paintEvent()

    Qt Code:
    1. QPainter painter(this);
    2. QPen pen;
    3. QFont font("Arial", 72, 50, true);
    4.  
    5. pen.setWidth(2);
    6. pen.setColor(Qt::red);
    7. painter.setFont(font);
    8. painter.setPen(pen);
    9.  
    10. path.addText(50, 132, font, "Hello Outline Text"); //Adjust the position
    11. painter.drawPath(path);
    To copy to clipboard, switch view to plain text mode 

    For further reading refer documentation

Similar Threads

  1. QSS, trying to make the text on the combo box WHITE not black.
    By technoViking in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2010, 00:57
  2. Image conversion black and white
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2010, 02:21
  3. İmage Convert Black and White
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2010, 06:08
  4. Displaying Text on Black and White Screen
    By Stobie in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 8th December 2009, 02:03
  5. Printing plot black & white
    By maybe78 in forum Qwt
    Replies: 4
    Last Post: 9th October 2009, 08:39

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.