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. Setting a backgroung image for a QPushButton does not work

Setting a backgroung image for a QPushButton does not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 948 Views
  • 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.
  • Q Offline
    Q Offline
    queenofdenile
    wrote on 14 Apr 2023, 20:22 last edited by
    #1

    Hi everyone,

    I'm trying to set a background image for a QPushButton. I want the image to cover the entire button, I do not want to set it as the button's icon. I tried:

    ui->pushButton->setStyleSheet("QPushButton { border-image:url(:/startProcessingButton) stretch;}");
    ui->pushButton->setFlat(true);
    ui->pushButton->setAutoFillBackground(true);

    I read about using border-image instead of background-image everywhere on the forum, but it doesn't seem to do the trick for me. It just displays a very tiny image in the center of the button that refuses to stretch to the size of the button. I also tried setting border-image-width and border-image-height, but that didn't take any effect.

    I am new to Qt, so I would really appreciate your help.

    C J 2 Replies Last reply 14 Apr 2023, 20:30
    0
    • Q queenofdenile
      14 Apr 2023, 20:22

      Hi everyone,

      I'm trying to set a background image for a QPushButton. I want the image to cover the entire button, I do not want to set it as the button's icon. I tried:

      ui->pushButton->setStyleSheet("QPushButton { border-image:url(:/startProcessingButton) stretch;}");
      ui->pushButton->setFlat(true);
      ui->pushButton->setAutoFillBackground(true);

      I read about using border-image instead of background-image everywhere on the forum, but it doesn't seem to do the trick for me. It just displays a very tiny image in the center of the button that refuses to stretch to the size of the button. I also tried setting border-image-width and border-image-height, but that didn't take any effect.

      I am new to Qt, so I would really appreciate your help.

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Apr 2023, 20:30 last edited by
      #2

      @queenofdenile said in Setting a backgroung image for a QPushButton does not work:

      url(:/startProcessingButton)

      An image without an extension?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply 14 Apr 2023, 20:43
      0
      • C Christian Ehrlicher
        14 Apr 2023, 20:30

        @queenofdenile said in Setting a backgroung image for a QPushButton does not work:

        url(:/startProcessingButton)

        An image without an extension?

        Q Offline
        Q Offline
        queenofdenile
        wrote on 14 Apr 2023, 20:43 last edited by
        #3

        @Christian-Ehrlicher, thank you for your answer. The image is added to my resources folder, and that is just an alias. It is correct, because I used the same model for example to add my background image.

        1 Reply Last reply
        0
        • Q queenofdenile
          14 Apr 2023, 20:22

          Hi everyone,

          I'm trying to set a background image for a QPushButton. I want the image to cover the entire button, I do not want to set it as the button's icon. I tried:

          ui->pushButton->setStyleSheet("QPushButton { border-image:url(:/startProcessingButton) stretch;}");
          ui->pushButton->setFlat(true);
          ui->pushButton->setAutoFillBackground(true);

          I read about using border-image instead of background-image everywhere on the forum, but it doesn't seem to do the trick for me. It just displays a very tiny image in the center of the button that refuses to stretch to the size of the button. I also tried setting border-image-width and border-image-height, but that didn't take any effect.

          I am new to Qt, so I would really appreciate your help.

          J Offline
          J Offline
          JoeCFD
          wrote on 14 Apr 2023, 20:44 last edited by JoeCFD
          #4
          This post is deleted!
          Q 1 Reply Last reply 14 Apr 2023, 20:59
          0
          • J JoeCFD
            14 Apr 2023, 20:44

            This post is deleted!

            Q Offline
            Q Offline
            queenofdenile
            wrote on 14 Apr 2023, 20:59 last edited by
            #5

            @JoeCFD thank you very much for your answer.

            I think it does find the path to the image, because it displays it in the middle of the button, only it's not the size I want (the white background is the background of my actual button, and the blue thing in the middle is my image):

            7b371163-3048-4546-9cdc-1c3aa850bbfe-image.png

            J 1 Reply Last reply 14 Apr 2023, 21:37
            0
            • Q queenofdenile
              14 Apr 2023, 20:59

              @JoeCFD thank you very much for your answer.

              I think it does find the path to the image, because it displays it in the middle of the button, only it's not the size I want (the white background is the background of my actual button, and the blue thing in the middle is my image):

              7b371163-3048-4546-9cdc-1c3aa850bbfe-image.png

              J Offline
              J Offline
              JoeCFD
              wrote on 14 Apr 2023, 21:37 last edited by JoeCFD
              #6

              @queenofdenile Mine works. Can you try to set the size of the button first and then style sheet? That is what I did.

              Q 1 Reply Last reply 17 Apr 2023, 13:54
              0
              • J JoeCFD
                14 Apr 2023, 21:37

                @queenofdenile Mine works. Can you try to set the size of the button first and then style sheet? That is what I did.

                Q Offline
                Q Offline
                queenofdenile
                wrote on 17 Apr 2023, 13:54 last edited by
                #7

                @JoeCFD I tried to set the button size first and I am still getting the same behavior. I was wondering if you could share your code, I am sure I'm missing something obvious. Many thanks!

                1 Reply Last reply
                0

                3/7

                14 Apr 2023, 20:43

                • Login

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