软件在使用时有时候需要对比强烈的提示一些内容或者其它操作,要将整个主界面变暗,然后提示的内容不变,就会显示很突出的效果
实现方式:
整个界面变暗使用一张带透明的黑色图片或者画一个变暗背景
代码:
QWidget *m_widget = new QWidget(this);
m_widget->resize (this->width(), this->height());
m_widget->move (0,0);
QPalette pal(m_widget->palette());
m_widget->setStyleSheet("background-color:rgba(0, 0, 0, 60%);border-radius: 20px;");//透明如果主界面是圆角就要圆角
m_widget->setAutoFillBackground(true);
m_widget->setPalette(pal);
m_widget->show();
这样会在整个界面增加一个变暗的效果,然后再增加自己的提示对话框
最终代码:
QWidget *m_widget;//头文件中申明
//阴影 0取消 1为显示
void Core::shadow_use(int state)
{
static int a = 0;
if(state == 0)
{
m_widget->hide();
}
else if(state == 1)
{
//有多个时隐藏显示的阴影
if(m_widget != NULL && a == 1)
{
if(m_widget->isHidden())
{
int a = 0;
}
else
{
m_widget->hide();
}
}
m_widget = new QWidget(this);
m_widget->resize (this->width(), this->height());
m_widget->move (0,0);
QPalette pal(m_widget->palette());
m_widget->setStyleSheet("background-color:rgba(0, 0, 0, 60%);border-radius: 20px;");
m_widget->setAutoFillBackground(true);
m_widget->setPalette(pal);
m_widget->show();
a = 1;
}
}
使用流程:
shadow_use(1);//显示
然后弹出对话框或者进度条啥的
shadow_use(0);//隐藏,然后根据弹出的对话框结果和进度来隐藏