[Solved] QPushButton background picture with text

  • What I'm trying to do is create a button that displays a background picture with a text message on top. This piece of code is the constructor for my UI form, and it works fine:
    FirstForm::FirstForm(QWidget parent) :
    ui(new Ui::FirstForm)

    setStyleSheet("QLabel { border-radius: 20px; border: 2px solid #000000 }"
    "QPushButton { border-radius: 20px; border: 2px solid #0000FF; background-color: #FFFF00 }");
    QPixmap pixmap(":/Icons/User_Icon.png");
    QPalette palette;
    QPushButton *myButton = new QPushButton(this);
    palette.setBrush(myButton->backgroundRole(), QBrush(pixmap));
    myButton->setGeometry(670, 20, 86, 91);
    myButton->setFont(QFont("Mufferaw", 12, 50));
    myButton->setText("Sign In");

    I have several other pushbuttons defined on the UI form, and I'd like all of them all to have the style in the setStyleSheet section except for myButton. When I uncomment the setStyleSheet section, though, myButton doesn't display the background image (I just see the yellow background with the text). What do I have to do to get myButton to display properly while keeping the setStyleSheet section?

  • You can also set the background image with the stylesheet.
    You could also assign the stylesheet for every button.
    You could also use one stylesheet and declare myButton in the stylesheet and do some specific styling for this element only.
    You can use (kind of) CSS in the stylesheet, so use the 'power' of CSS! ;-)

    @QPushButton {
    background: black;
    color: white;
    #myButton {
    background: yellow;
    color: red;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.