PDA

View Full Version : Can QDockWidget be semi-transparent?



PolyVox
29th September 2008, 23:24
Hi Guys,

I can't seem to make my QDockWidget semi-transparent, and was wondering if it is possible? Here is my code:


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

//The main window just has a label in it's center
QMainWindow* mainWindow = new QMainWindow(0,0);
QLabel* mainLabel = new QLabel("Main Widget Label", mainWindow);
mainWindow->setCentralWidget(mainLabel);

//This dock window should appear 50% transparent. Instead it is fully opaque.
QDockWidget* dockWidget = new QDockWidget("Dock Widget", mainWindow);
QPushButton* pushButton = new QPushButton("Dock Widget Button", dockWidget);
dockWidget->setWidget(pushButton);
dockWidget->setWindowOpacity(0.5);
mainWindow->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);

//Just to show transparent can work, this seperate widget does show up 50% transparent.
QWidget *w = new QWidget(0,0);
QPushButton* floatingPushButton = new QPushButton("Floating Widget Button", w);
w->setWindowOpacity(0.5);

//Show the windows.
mainWindow->show();
w->show();

return app.exec();
}

As you can see from the screenshot below, the regular QWidget is semi-transparent but the QDockWidget is not:

http://www.david-williams.info/linked_from_web/qt/transparency-problem.png

Is this expected? I guess it is conceivable that QDockWidgets are drawn differently (I notice they have different window decorations). Anyone know a way around this, or what I am doing wrong?

Thanks,

David

Lykurg
30th September 2008, 18:02
Hi,

it's possible to make an QDockWidget semi-transparent. But in your example the problem is, that after making the QDockWidget floating the opacity is set to 100%. So you have to detected if the widget is maked floating via the signal
dockLocationChanged ( Qt::DockWidgetArea area ) and set the opacity manually to 50%.


Lykurg

PolyVox
30th September 2008, 23:48
Thanks, that did the trick! Shame this doesn't appear to be documented though. Oh, and for anyone else who has this problem the correct signal is actually:

QDockWidget::topLevelChanged(bool)

spagatoni
17th March 2009, 21:09
Does this apply to docked QDockWidgets?

Currently on signal "topLevelChanged" I setWindowOpacity(.5) and this doesn't do anything when I "dock" the widget. When it is floating it shows it as having transparency.

Any help would be awesome and thanks for taking time to read this.