一、問題
Linux系統下Qt窗口的透明效果成了黑色,但同樣的代碼在windows機子上有透明效果,主要是修改系統的配置,僅在centos6.3及其以上版本實驗過。其他系統可以相應配置。
二、問題運行圖
(1)Qt透明效果,centos下運行都會出現如下圖1所示。
三、解決方法
(1)窗口透明,窗口上的控件不透明,其代碼設置
- setAutoFillBackground(false); //這個不設置的話就背景變黑
- setWindowFlags(Qt::FramelessWindowHint);
- setAttribute(Qt::WA_TranslucentBackground,true);
- //setWindowOpacity(1); //窗口及控件都半透明,其取值為0-1(0是全透明,1是不透明)
- //setAttribute(Qt::WA_NoSystemBackground, true);
- //setStyleSheet("background-color:transparent;");
- //QWidget::setAttribute(Qt::WA_NoBackground, true);
- //QPalette pal = palette();
- //pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
- //setPalette(pal);
(2)Qt參考文檔
Platform notes:
- X11: This feature relies on the use of an X server that supports ARGB visuals and a compositing window manager.
- Windows: The widget needs to have the Qt::FramelessWindowHintwindow flag set for the translucency to work.
平台注意事項:
X11(linux):此特性依賴於能提供支持ARGB視覺效果和復合式視窗管理的X服務的功能開啟。
Windows:此控件需要設置窗口標志
Qt::FramelessWindowHint才能開啟透明功能。
接着修改Centos的配置,是透明效果生效。
(3)修改Centos配置


改動后的效果(透明)(截圖的背景色是白色):

http://blog.csdn.net/taiyang1987912/article/details/37037657