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. [Qt5] Can't set WA_TranslucentBackground to a QWidget ?
Forum Updated to NodeBB v4.3 + New Features

[Qt5] Can't set WA_TranslucentBackground to a QWidget ?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 14.5k 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.
  • W Offline
    W Offline
    weiyuemin
    wrote on last edited by
    #1

    I'm porting my programs to Qt5, but failed to show a translucent window. It works well when I'm using Qt 4.8

    Here is the code:

    @
    #include <QtWidgets/QApplication>
    #include <QtDeclarative/qdeclarative.h>
    #include <QtDeclarative/qdeclarativeview.h>
    #include <QLabel>

    int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(new QApplication(argc, argv));

    QWidget w;
    w.resize(500, 400);
    QPalette palette = w.palette();
    palette.setColor(QPalette::Base, Qt::transparent);
    w.setPalette(palette);
    w.setAttribute(Qt::WA_TranslucentBackground); //this line will make the program crash
    w.setWindowFlags(Qt::FramelessWindowHint);

    QLabel *label = new QLabel("Some label", &w);
    label->setObjectName("label");

    w.show();

    return app->exec();
    }
    @

    If I set the attribute: w.setAttribute(Qt::WA_TranslucentBackground);
    then the program will crash when executing app->exec();
    at _heap_alloc, with a tremendous size. It seems heap corrupted.

    But the code is OK in Qt 4.8.

    So how to implement a translucent window in Qt5? Or is there some issue I neglected ?

    1 Reply Last reply
    0
    • W Offline
      W Offline
      weiyuemin
      wrote on last edited by
      #2

      more info: I'm on Windows XP

      1 Reply Last reply
      0
      • S Offline
        S Offline
        superpacko
        wrote on last edited by
        #3

        I'm on Windows 7 and I'm having an issue with Qt::WA_TranslucentBackground as well. My app is not crashing, but if I set that attribute to true, then my widget is not displayed at all. Im setting the widget as
        4.8.1 was working just fine, didnt change anything, there must be something broken in Qt5 with that behaviour.

        Any ideas?

        I hope someone can fix this since im porting my application to Qt5 and this tricks is one of the "nice" visual details of it and is not working now.

        ej:
        @Widget::Widget(QWidget *parent) :
        QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
        {
        ui.setupUi(this);

        setAttribute(Qt::WA_TranslucentBackground,true);
        
        QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect();
        effect->setXOffset(0.0);
        effect->setYOffset(0.0);
        effect->setColor(Qt::gray);
        effect->setBlurRadius(6);
        
        ui.Container->setGraphicsEffect(effect);
        
        shorcut.setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Space));
        
        setVisible(false);
        
        connect(&shorcut, SIGNAL(activated()), this, SLOT(shortcutActivated()));
        

        }

        void Widget::shortcutActivated()
        {
        if (ui.Container->isVisible())
        ui.Container->hide();
        else
        {
        show();
        raise();
        activateWindow();
        ui.Container->show();
        }

        qDebug() << "shortcut activated!"; //--&gt; i get this, but the widget is not been displayed
        

        }
        @

        1 Reply Last reply
        0
        • W Offline
          W Offline
          weiyuemin
          wrote on last edited by
          #4

          I tried Qt5.0.1 yesterday, and Qt::WA_TranslucentBackground works right. ( w.setAttribute(Qt::WA_TranslucentBackground); )

          It seems it was fixed in 5.0.1

          1 Reply Last reply
          0
          • S Offline
            S Offline
            superpacko
            wrote on last edited by
            #5

            mmm i have Desktop 5.0.1, i've created a simple project that it only displays this widget. Nothing is displayed when using translucent background:

            @Form::Form(QWidget *parent) :
            QWidget(parent, Qt::FramelessWindowHint)
            {
            ui.setupUi(this);
            setAttribute(Qt::WA_TranslucentBackground);
            }@

            Form is a QWidget containing another QWidget with a blue background. As simple as that.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              superpacko
              wrote on last edited by
              #6

              Ok i think i found something. This is probably a bug.
              I removed the setAttribute(Qt::wA_TranslucentBackground); and of course that works fine and the widget is displayed. Then i changed the style of the container widget to have a background color with an alpha channel of 100. BUM! widget is not longer been displayed.

              As long as you use 255 o simple rgb color, it works, but when you introduce some transparency, alpha channel lower than 255, that breaks the display. So it might have nothing to do with the Qt::WA_TranslucentBackground, but with the alpha channel of the top level widget.

              so, to sum up:

              1. using Qt::FramelessWindowHint + Qt::WA_TranslucentBackground: QWidget not displayed
              2. using Qt::FramelessWindowHint + top level widget's background color and alpha channel < 255: QWidget not displayed
              3. using top level widget's background color and alpha channel < 255: QWidget not displayed
              4. using Qt::FramelessWindowHint + top level widget's background color and alpha channel = 255: OK QWidget displayed
              1 Reply Last reply
              0
              • W Offline
                W Offline
                weiyuemin
                wrote on last edited by
                #7

                I'm on WinXP and have no problem using a single QWidget with Qt::WA_TranslucentBackground.

                Would you please paste the complete code of the minimal test to reproduce the issue?

                I suggest you report a bug at https://bugreports.qt-project.org/secure/Dashboard.jspa

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  superpacko
                  wrote on last edited by
                  #8

                  Ok... i got it working... but there's some strange thing going on.

                  If i create the widget manually everything is good. But i was using a simple ui file created with designer. Then if i set the attribute after calling ui->setupUi(this) then the widget is not displayed. If i set the attribute before calling setupUi, that works. Strange isnt it?

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    craigus
                    wrote on last edited by
                    #9

                    I have the same issue on win7. My painting is actually done in a contained QGraphicsView and the QDialog Container widget is transparent to get rounded corners. I verified that removing the call to setupUi (which in my case does nothing anyway) fixes the issue (the widget now displays the contained QGraphicsView).

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      superpacko
                      wrote on last edited by
                      #10

                      Yep, aparently setAttribute has to be called before calling setupUi, at least for the WA_TranslucentBackground atribute. Not sure why, maybe someone from Qt have a better explanation.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        superpacko
                        wrote on last edited by
                        #11

                        Im still having issues when using translucent attribute. My app is like a menu bar that when you click on a button it slides down a widget that gives you options depending on the button you've clicked.

                        When i click any button, suddenly the rest of the buttons dissapear, as well as the shadow effect that surrounds the widget. i can only get back to normal if i repaint the whole app or minimize/restore it. Also because of this weird behaviour sometimes i get a crash because im storing those sliding widgets in QScoopedPointers and when trying to acces them when they are supposed to be displayed the pointer is null. This doesnt happen at all if i disable the WA_TransulcentBackground attribute.

                        Something has changed from 4.8.1 to 5.0.1 on the widget handling when translucency is used. since everything was working fine. And this is a pretty annoying bug, that needs to be fixed, otherwhise all applications using this thing will eventually suffer from weird behaviour.

                        Is anyone from the Qt team taking a look in this forums? Help will be much appreciated.

                        1 Reply Last reply
                        0
                        • Z Offline
                          Z Offline
                          zhaoweiwei
                          wrote on last edited by
                          #12

                          Thanks for superpacko. Pefect resolution!!
                          [quote author="superpacko" date="1361394223"]Ok... i got it working... but there's some strange thing going on.

                          If i create the widget manually everything is good. But i was using a simple ui file created with designer. Then if i set the attribute after calling ui->setupUi(this) then the widget is not displayed. If i set the attribute before calling setupUi, that works. Strange isnt it?[/quote]

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            amonR
                            wrote on last edited by
                            #13

                            In my case, after or before it doesn't change anything! I still have a black background. Help please!!!!

                            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