Results 1 to 7 of 7

Thread: QSS problem on a QPushButton

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QSS problem on a QPushButton

    Hi, i have a problem using QSS on a QWidget.

    I would like to display a image above the text of a QPushButton.
    Actually my code is :
    Qt Code:
    1. // Constructor() {
    2. setText("Push Button Text");
    3.  
    4. QString styleSheet;
    5. styleSheet.append("QPushButton{");
    6. styleSheet.append("background-image: url(images/image.jpg);");
    7. styleSheet.append("background-repeat: no-repeat;");
    8. styleSheet.append("background-position: center;");
    9. styleSheet.append("}");
    10. setStyleSheet(styleSheet);
    11. // }
    To copy to clipboard, switch view to plain text mode 

    Because it's background-image, it's under the text...
    If i change "background-image" to "image", the image does not appear !

    how to put the image above the text, centered on the QPushButton please ? ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSS problem on a QPushButton

    It's not possible. Use QToolButton instead.

  3. #3
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSS problem on a QPushButton

    Oh thanks...but sorry i would like to display a small image on a text, not above sorry !
    i mean i would like to display the text, and at the same place, the small image on the text...

    sorry for my english, i thought that it was "above" instead of "on" !

    how can i do that please?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSS problem on a QPushButton

    Your code works fine for me. Make sure, you don't replace the the style sheet anywhere else and that your path to the icon is valid. (If it is a resource you mitt the ":".)

  5. #5
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSS problem on a QPushButton

    i only set the steel sheet here, in the constructor. My code works fine to display a image under a text, not the contrary (the text under the image) and that's my problem

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSS problem on a QPushButton

    Quote Originally Posted by ber0y View Post
    i only set the steel sheet here, in the constructor. My code works fine to display a image under a text, not the contrary (the text under the image) and that's my problem
    Ok, know I understand you right! Sorry, took some time...

    That behavior you can't realize with css, you have to subclass QPushButton. But that's easy. Just modify the paint event like:
    Qt Code:
    1. MyButton::paintEvent(QPaintEvent *event)
    2. {
    3. QPushButton::paintEvent(event); // let Qt draw the regular button
    4. // and now just paint your image on the button (and text)
    5. QPixmap pixmap(":myPixmap.png");
    6. QPoint point = event->rect().center();
    7. point.translate(pixmap.rect().center() * (-1));
    8. QPainter p(this);
    9. p.drawPixmap(point, pixmap);
    10. }
    To copy to clipboard, switch view to plain text mode 
    not tested but should work...

  7. The following user says thank you to Lykurg for this useful post:

    ber0y (21st July 2009)

  8. #7
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Thumbs up Re: QSS problem on a QPushButton

    Quote Originally Posted by Lykurg View Post
    That behavior you can't realize with css, you have to subclass QPushButton. But that's easy. Just modify the paint event like:
    Qt Code:
    1. MyButton::paintEvent(QPaintEvent *event)
    2. {
    3. QPoint point = event->rect().center();
    4. point.translate(pixmap.rect().center() * (-1));
    5. }
    To copy to clipboard, switch view to plain text mode 
    It's perfect ! Exactly what i wanted
    Thanks!

    Just a thing : QPoint does not have the method translate(), but i try to use setX and setY which allow to move the image i guess.

    Thanks again


    PS :This code works fine

    Qt Code:
    1. void PushButtonPictured::paintEvent(QPaintEvent *event)
    2. {
    3. QPushButton::paintEvent(event);
    4.  
    5. QPixmap pixmap(m_imageFileName);
    6. QPoint point = (event->rect()).center();
    7. point.setX(point.x() - pixmap.rect().center().x());
    8. point.setY(point.y() - pixmap.rect().center().y());
    9.  
    10. QPainter painter(this);
    11. painter.drawPixmap(point, pixmap);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ber0y; 21st July 2009 at 08:24. Reason: find solution

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. Problem with enabling QPushButton!
    By Krish in forum Newbie
    Replies: 6
    Last Post: 5th March 2008, 14:49
  3. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  4. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 21:48
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

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.