想要做一個像 Word 里面可以半透明的懸停窗口
網上扒了半天,終于找到解決方法了咱揍。
就是它颖榜!
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參數(shù)是透明度等級,從1.0到0.0(從不透明到完全透明)煤裙。默認情況下掩完,此屬性的值為1。
當然還有一個獲得當前窗口透明度等級的函數(shù)
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));
}