您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Qt随笔 - 窗口及其控件透明

Qt随笔 - 窗口及其控件透明

来源:伴沃教育

想要做一个像 Word 里面可以半透明的悬停窗口


Paste_Image.png

网上扒了半天,终于找到解决方法了。

就是它!

void setWindowOpacity(qreal level)

官方说法:

windowOpacity : double
This property holds the level of opacity for the window.
The valid range of opacity is from 1.0 (completely opaque) to 0.0 (completely transparent).
By default the value of this property is 1.0.
This feature is available on Embedded Linux, macOS, Windows, and X11 platforms that support the Composite extension.
Note: On X11 you need to have a composite manager running, and the X11 specific 
_NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using.
Warning: Changing this property from opaque to transparent might issue a paint event that needs to be 
processed before the window is displayed correctly. This affects mainly the use of QPixmap::grabWindow(). 
Also note that semi-transparent windows update and resize significantly slower than opaque windows.

这个level参数是透明度等级,从1.0到0.0(从不透明到完全透明)。默认情况下,此属性的值为1。

当然还有一个获得当前窗口透明度等级的函数

qreal windowOpacity() const

来看个Demo:

void MainWindow::on_horizontalScrollBar_sliderMoved(int position)
{
    setWindowOpacity(position*1.0/10);
    ui->label_level->setText("level = "+QString::number(position*1.0/10));
}

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务