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. Background image stretch and opacity not working

Background image stretch and opacity not working

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 3.6k 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.
  • H Offline
    H Offline
    Hmmm
    wrote on last edited by
    #1

    I'm trying to style my app a little bit. So I added a background image and I want it to be stretchy with no repeat and with a little opacity. I'm using this code:

    this->centralWidget()->setStyleSheet("background-image: url(:/image.jpg) 0 0 0 0; stretch stretch; opacity: 50; background-repeat: no-repeat; background-position: center;");
    

    But the opacity and stretching seems not to work.

    J.HilkJ 1 Reply Last reply
    1
    • H Hmmm

      I'm trying to style my app a little bit. So I added a background image and I want it to be stretchy with no repeat and with a little opacity. I'm using this code:

      this->centralWidget()->setStyleSheet("background-image: url(:/image.jpg) 0 0 0 0; stretch stretch; opacity: 50; background-repeat: no-repeat; background-position: center;");
      

      But the opacity and stretching seems not to work.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @Hmmm and welcome

      take a look at this old thread
      https://forum.qt.io/topic/40151/solved-scaled-background-image-using-stylesheet/3

      I don't think much changed, so using border-image might do the trick for you


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      H 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        hi @Hmmm and welcome

        take a look at this old thread
        https://forum.qt.io/topic/40151/solved-scaled-background-image-using-stylesheet/3

        I don't think much changed, so using border-image might do the trick for you

        H Offline
        H Offline
        Hmmm
        wrote on last edited by
        #3

        @J.Hilk Thank you for your answer. It did solve stretching problem but not opacity. Also it added the same image to my buttons. I tried solving it with:

            ui->myButton->setStyleSheet("background-color: white; ");
        

        But it doesn't work, image is still there.

        Pl45m4P 1 Reply Last reply
        0
        • H Hmmm

          @J.Hilk Thank you for your answer. It did solve stretching problem but not opacity. Also it added the same image to my buttons. I tried solving it with:

              ui->myButton->setStyleSheet("background-color: white; ");
          

          But it doesn't work, image is still there.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @Hmmm

          If you dont define an object / class where to set the stylesheet, it will use the current one and all of its childs... So when you set the bg-image to QMainWindow without using

          ("QMainWindow { border-image: img.jpg }")
          

          ... it will apply the stylesheet (background, in your case) to all valid child widgets from your QMainWindow. Every MessageBox, every PushButton, every Label... has the same background as QMainWindow then.

          EDIT:
          Ofc you have to use the correct class (+ object name) for your central widget, if you want to set the BG to your CentralWidget only (not whole MainWindow BG) and there are other types of widgets inside your MainWindow (Menus, ToolBars...etc)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2

          • Login

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