Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Qt5] Can't set WA_TranslucentBackground to a QWidget ?
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:
int main(int argc, char *argv)
QScopedPointer<QApplication> app(new QApplication(argc, argv));
QPalette palette = w.palette();
w.setAttribute(Qt::WA_TranslucentBackground); //this line will make the program crash
QLabel *label = new QLabel("Some label", &w);
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 ?
more info: I'm on Windows XP
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.
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.
@Widget::Widget(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
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()));
qDebug() << "shortcut activated!"; //--> i get this, but the widget is not been displayed
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
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) :
Form is a QWidget containing another QWidget with a blue background. As simple as that.
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:
- using Qt::FramelessWindowHint + Qt::WA_TranslucentBackground: QWidget not displayed
- using Qt::FramelessWindowHint + top level widget's background color and alpha channel < 255: QWidget not displayed
- using top level widget's background color and alpha channel < 255: QWidget not displayed
- using Qt::FramelessWindowHint + top level widget's background color and alpha channel = 255: OK QWidget displayed
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
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?
craigus last edited by
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).
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.
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.
zhaoweiwei last edited by
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]
amonR last edited by
In my case, after or before it doesn't change anything! I still have a black background. Help please!!!!