QT application programming: realization of translucent mask window

1. Environmental introduction

Operating system introduction: win10 64 bit

QT version:  5.12.6

2. Requirements introduction

When we display some modal dialog boxes, we often need to darken the background color of the dialog box to achieve the effect of highlighting the current dialog box.

For example: the parent window of the dialog box is darkened except for the title bar, and the display effect of the dialog box is emphasized in comparison with the parent window.

This design is more common in web pages. When the user clicks a button such as purchase, a shopping list confirmation dialog box will pop up on the page, and the content outside the dialog box will be processed with an effect similar to the figure, so that the user can focus In the dialog itself.

 

Third, the implementation code

/*
日期: 2021-01-02
作者: DS小龙哥
环境: win10 QT5.12.6 MinGW32
功能: 打开遮罩窗体
*/
void Widget::on_pushButton_clicked()
{
    QWidget mask_window(this);
    //设置窗体的背景色,这里的百分比就是透明度
    mask_window.setStyleSheet(QString("background-color: rgba(0, 0, 0, 20%);"));
    mask_window.setGeometry(this->rect()); //获取父窗体的几何形状设置当前窗口
    mask_window.show();
    //对话框
    QMessageBox::information(this,"提示","遮罩框已打开",QMessageBox::Ok,QMessageBox::Ok);
    //关闭窗口
    mask_window.close();
}

 

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/112100900