transparent window on both mac and windows?
-
i have a minimal project link click HERE, based on "windowflags" example
on mac, i can only get the desired effect if i do these steps:
- check "translucent background" (ON)
- check "no system background" (ON)
- un-check "translucent background" (OFF)
now i'm in the state where it looks like this:
that's great, that's exactly what i want. but when i go to resize it, the transparent area fills with junk.
On Windows, i can't get ANYTHING to cause the window to be transparent. I tried checking "frameless window" and "translucent background", as many people suggest, but that doesn't work. plus i need a widow i can drag around.
anyone have any experience with this?
i don't mind platform specific code if necessary, this is just mac and windows.
-
Hi @davecotter
I have an app/standalone process that I launch as a splash screen, that has transparent background but it is not resizable.
For the QWidget version I set the following flags & attributes:
this->setWindowFlags( Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::WindowTransparentForInput); this->setAttribute(Qt::WA_TranslucentBackground,true);
-
Well, I don’t want it always on top, and I certainly do not want it frameless, and I need it to be resizable.
Can you run the example project I provided, and perhaps edit the code to make it work? I’ve done everything I can think of
it doesn’t seem like QT actually supports transparent windows even though the documentation says that it does?
-
I didn't see the link 🙈
Well yes, your way described seems to work, some what, when I moved the window from gpu rendered screen to cpu rendered one it broke completelyWhat also works, is FramelessWindowHint & TranslucentBackground
but you said you don't want frameless 🤔
Not sure if I can help you :(
-
@J-Hilk did you do that on mac or windows? did you use my app to do it? what steps did you take, exactly?
-
Qt::WA_TranslucentBackground
requiresQt::FramelessWindowHint
to work on Windows.If that didn't work for you then you are setting the attribute and flag too late. Be sure to call the attributes very early, before show, the first thing you do in the constructor.
If you decide to go this route then also have a look at
QWindow::startSystemResize
andQWindow::startSystemMove
which might help make this less painful.#include <QtWidgets> class MyWidget : public QWidget { public: MyWidget() { setAttribute(Qt::WA_TranslucentBackground, true); setWindowFlag(Qt::FramelessWindowHint, true); auto lay = new QVBoxLayout(this); lay->setSpacing(10); lay->addWidget(new QPushButton("1")); lay->addWidget(new QPushButton("2")); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget window; window.show(); return app.exec(); }
-
Let's now see
QWidget::setMask(region)
instead.
It's very powerful and doesn't require any special flags, you just set the region that you don't want as transparent.The downside is you need to calculate the regions yourself, so I would only use this for simple static layouts.
Also the title bar looks weird. There might be Win32 API to fix it though.
#include <QtWidgets> // Example of having a window with an hole, but still preserving title bar class MyWidget : public QWidget { public: MyWidget() { resize(1000, 1000); } void resizeEvent(QResizeEvent *ev) override { QWidget::resizeEvent(ev); QRect r = frameGeometry(); r.moveTopLeft(mapFromGlobal(r.topLeft()) ); QRect hole(0, 0, width() - 50, 100); hole.moveCenter(rect().center()); setMask(QRegion(r).subtracted(hole)); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget window; window.show(); return app.exec(); }