CentOS下Qt窗口透明效果失效,成黑色背景的问题

一、问题

       Linux系统下Qt窗口的透明效果成了黑色,但同样的代码在windows机子上有透明效果,主要是修改系统的配置,仅在centos6.3及其以上版本实验过。其他系统可以相应配置。

二、问题运行图

(1)Qt透明效果,centos下运行都会出现如下图1所示。

三、解决方法

(1)窗口透明,窗口上的控件不透明,其代码设置

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. setAutoFillBackground(false);  //这个不设置的话就背景变黑  
  2. setWindowFlags(Qt::FramelessWindowHint);  
  3. setAttribute(Qt::WA_TranslucentBackground,true);  
  4. //setWindowOpacity(1);  //窗口及控件都半透明,其取值为0-1(0是全透明,1是不透明)  
  5. //setAttribute(Qt::WA_NoSystemBackground, true);  
  6. //setStyleSheet("background-color:transparent;");  
  7. //QWidget::setAttribute(Qt::WA_NoBackground, true);  
  8. //QPalette pal = palette();   
  9. //pal.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));   
  10. //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配置
进入菜单栏的系统->首选项->桌面效果
修改为Complz,不过修改后系统会变得稍微有些卡,太耗内存了。
改动后的效果(透明)(截图的背景色是白色):

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

原文地址:https://www.cnblogs.com/findumars/p/5782021.html