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. QT opacity on background
Forum Updated to NodeBB v4.3 + New Features

QT opacity on background

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 814 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.
  • D Offline
    D Offline
    DevM
    wrote on last edited by DevM
    #1

    Hey,

    I'm new to QT and I have been struggling to put a QPixmap transparent, I currently have this code:

    void MainWindow::resizeEvent(QResizeEvent *event){
        //Gets background images and resizes it on window resize
        QPixmap backgroundImage(":/Images/Background/img.jpg");
        backgroundImage = backgroundImage.scaled(this->size(), Qt::IgnoreAspectRatio);
    
        QPalette p = palette();
        p.setBrush(QPalette::Background,backgroundImage);
        this->setPalette(p);
    
        //Calls base implementation
        QMainWindow::resizeEvent(event);
    }
    

    That resizes the image when moving the window around, but I have been unable to set that background image to have some kind of opacity. What is the best way to go about this, I am aware that Im not the only one that has had this problem before since I have searched around but their implementations didn't seem to work for me.

    Also if someone could explain the difference in using QPalette to paint versus QImage and the few others I have seen around that would be great as well, Im confused as why there are so many.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Do you mean opacity so that Desktop can be seen through the mainwindow?

      On windows 10 , i could only do that with

      setAttribute(Qt::WA_TranslucentBackground);
      setWindowFlags(Qt::FramelessWindowHint); // with borders, its always just black.
      

      void MainWindow::paintEvent(QPaintEvent *event)
      {
      QPainter p(this);
      p.setOpacity(0.3);
      p.drawPixmap(0, 0, QPixmap(":/bg.jpg"));
      }

      alt text

      D 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Do you mean opacity so that Desktop can be seen through the mainwindow?

        On windows 10 , i could only do that with

        setAttribute(Qt::WA_TranslucentBackground);
        setWindowFlags(Qt::FramelessWindowHint); // with borders, its always just black.
        

        void MainWindow::paintEvent(QPaintEvent *event)
        {
        QPainter p(this);
        p.setOpacity(0.3);
        p.drawPixmap(0, 0, QPixmap(":/bg.jpg"));
        }

        alt text

        D Offline
        D Offline
        DevM
        wrote on last edited by
        #3

        @mrjj

        I mean the image itself being transparent, I want to have this effect ( the image in the back )

        Screenshot_1.png image url)

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Ah, im not tried with QPalette::Background and transparent but if
          you combine the shown paintEvent and your resizeEvent, it should look
          just like that.

          backgroundImage should be a member so you scale it in resizeEvent and paintEvent paints it with
          p.setOpacity(0.3);

          D 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            Ah, im not tried with QPalette::Background and transparent but if
            you combine the shown paintEvent and your resizeEvent, it should look
            just like that.

            backgroundImage should be a member so you scale it in resizeEvent and paintEvent paints it with
            p.setOpacity(0.3);

            D Offline
            D Offline
            DevM
            wrote on last edited by DevM
            #5

            @mrjj

            Currently have it like this then

            void MainWindow::resizeEvent(QResizeEvent *event){
                //Gets background images and resizes it on window resize
                backgroundImage = backgroundImage.scaled(this->size(), Qt::IgnoreAspectRatio);
            
                /*QPalette p = palette();
                p.setBrush(QPalette::Background,backgroundImage);
                this->setPalette(p);*/
            
                //Calls base implementation
                QMainWindow::resizeEvent(event);
            }
            
            void MainWindow::paintEvent(QPaintEvent *event){
                QPainter p(this);
                p.setOpacity(0.5);
                p.drawPixmap(0,0,QPixmap(backgroundImage));
            }
            

            for some reason though the image on resize now acts weird

            Okay:
            Screenshot_2.png

            Not okay(after resizing):
            Screenshot_4.png

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              Hi
              I think it comes from scaling the scaled image. ( Scale the scaled so to speak)
              Else i don't know why it would look that odd suddenly.

              So try with keeping the original image in new variable org_backgroundImage
              and do

              backgroundImage = org_backgroundImage.scaled(this->size(), Qt::IgnoreAspectRatio);

              so each time we scale from org image and not the last scaled version.

              D 1 Reply Last reply
              2
              • mrjjM mrjj

                Hi
                I think it comes from scaling the scaled image. ( Scale the scaled so to speak)
                Else i don't know why it would look that odd suddenly.

                So try with keeping the original image in new variable org_backgroundImage
                and do

                backgroundImage = org_backgroundImage.scaled(this->size(), Qt::IgnoreAspectRatio);

                so each time we scale from org image and not the last scaled version.

                D Offline
                D Offline
                DevM
                wrote on last edited by
                #7

                @mrjj

                Yep that makes sense that it was what was happening, thank you very much for the help!

                1 Reply Last reply
                1

                • Login

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