transparent Widget
-
I examined many ways,
But I can't make a transparent tabwidget (In Linux) :
-
It's a 3 step process.
-
Make the window widget transparent using attribute
Qt::WA_TranslucentBackground
, which gives you something like in your picture:
-
Make the tab header transparent. You can do that with stylesheet
QTabBar::tab { background: transparent; }
Note that this is stylingQTabBar
, notQTabWidget
.
You can modify the stylesheet to add the borders of the tab back if you like. -
Make the tab content transparent. Note that
QTabWidget
itself doesn't have a background. It's the background of the widget inside it that you need to make transparent. The easiest way to do this is to tell Qt that the paint event takes care of all the painting and it does not need to fill the background with default color i.e. useQt::WA_OpaquePaintEvent
attribute.
So the entire example looks like this:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget* tab_content = new QWidget(); tab_content->setAttribute(Qt::WA_OpaquePaintEvent); //makes the widget transparent QTabWidget* tw = new QTabWidget(); tw->addTab(tab_content, "Example"); tw->setStyleSheet("QTabBar::tab { background: transparent; }"); //makes the header transparent QWidget w; w.setAttribute(Qt::WA_TranslucentBackground); //makes the window transparent w.setLayout(new QVBoxLayout()); w.layout()->addWidget(tw); w.show(); return a.exec(); }
-
-
@Chris-Kawa
Hi Dear Chris Kawa
The method that you mentioned in previous post helped me a lot.
Thanks a lot.
But I tried to make a semi-transparent canvas (of qwtplot) . The canvas inherit from QFrame. I tried many days but I still can't do that.
A black background will appear instead of parent widget behind it.
QwtPlotCanvas Class Reference -
@Chris-Kawa, I tried your code but it does not work on Windows 10 and Qt 5.15.2. Why? Maybe does somebody else know it?
-
@8Observer8 Were you able to resolve this issue? I'm having weird behavior even though i'm following the instructions, so not sure what am I doing wrong?
-
@Chris-Kawa Your code works nicely on my local computer. But the transparent feature is gone after I copy the executable from my machine to another computer. Any thoughts?
same OS: Ubuntu 22.04
same Qt version: 5.15.3 -
@JoeCFD you people may find this helpfull
https://github.com/jordanprog86/QtTransparentWindow.git
-
@Ronel_qtmaster thanks for your post. Your code is not working on Lubuntu with LXQt desktop as well for Qt5/6. Chris Kawa's test code has the same issue.
The following example works
https://doc.qt.io/qt-5/qtwidgets-widgets-shapedclock-example.html