Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QPushButton background picture with text
Forum Updated to NodeBB v4.3 + New Features

[Solved] QPushButton background picture with text

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 6.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Endless
    wrote on last edited by
    #1

    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) :
    QWidget(parent),
    ui(new Ui::FirstForm)
    {
    ui->setupUi(this);
    /

    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->setFlat(true);
    myButton->setAutoFillBackground(true);
    myButton->setPalette(palette);
    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sportlover
      wrote on last edited by
      #2

      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! ;-)

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved