View Full Version : QSS problem on a QPushButton
ber0y
20th July 2009, 14:51
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 :
// Constructor() {
setText("Push Button Text");
QString styleSheet;
styleSheet.append("QPushButton{");
styleSheet.append("background-image: url(images/image.jpg);");
styleSheet.append("background-repeat: no-repeat;");
styleSheet.append("background-position: center;");
styleSheet.append("}");
setStyleSheet(styleSheet);
// }
Because it's background-image, it's under the text...
If i change "background-image" to "image", the image does not appear ! :eek:
how to put the image above the text, centered on the QPushButton please ? :confused: ?
Lykurg
20th July 2009, 15:00
It's not possible. Use QToolButton instead.
ber0y
20th July 2009, 15:22
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" ! :o
how can i do that please?
Lykurg
20th July 2009, 15:37
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 ":".)
ber0y
20th July 2009, 15:48
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
Lykurg
20th July 2009, 17:25
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...:rolleyes:
That behavior you can't realize with css, you have to subclass QPushButton. But that's easy. Just modify the paint event like:
MyButton::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event); // let Qt draw the regular button
// and now just paint your image on the button (and text)
QPixmap pixmap(":myPixmap.png");
QPoint point = event->rect().center();
point.translate(pixmap.rect().center() * (-1));
QPainter p(this);
p.drawPixmap(point, pixmap);
}
not tested but should work...
ber0y
21st July 2009, 07:56
That behavior you can't realize with css, you have to subclass QPushButton. But that's easy. Just modify the paint event like:
MyButton::paintEvent(QPaintEvent *event)
{
QPoint point = event->rect().center();
point.translate(pixmap.rect().center() * (-1));
}
It's perfect ! Exactly what i wanted :p
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
void PushButtonPictured::paintEvent(QPaintEvent *event)
{
QPushButton::paintEvent(event);
QPixmap pixmap(m_imageFileName);
QPoint point = (event->rect()).center();
point.setX(point.x() - pixmap.rect().center().x());
point.setY(point.y() - pixmap.rect().center().y());
QPainter painter(this);
painter.drawPixmap(point, pixmap);
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.