Qt笔记之-- 一种利用遮罩窗口突出显示弹窗的方法
简介本文将介绍如何利用Qt实现高亮弹窗效果窗口亮度调节注意不是透明度即在弹出窗口时主窗口亮度变暗弹出窗口高亮显示其实弹出窗口是正常显示的只是背景变暗了突出显示了弹窗下面是动态显示效果图。实现上面所示的效果本demo一共要用到三个窗口一个是主窗口一个是遮罩窗口另一个是弹窗。其原理是在窗口弹出时先将遮罩显示再显示弹窗。其中遮罩窗口背景色设置为黑色透明度为0.6。实现本文所列举的代码已经通过编译编译环境windows 10Qt 5.9.5MSVC2017 64位建立工程利用Qt Creator创建QWidget工程带UI文件UI文件如下main.cpp#include mainwindow.h #include QApplication int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }弹窗类dialog.h#ifndef DIALOG_H #define DIALOG_H #include QDialog class Dialog; class Dialog:public QDialog{ Q_OBJECT public: Dialog(); ~Dialog(); protected: void closeEvent(QCloseEvent *e){ Q_UNUSED(e); emit Closed(); } signals: void Closed(); }; #endifdialog.cpp#include dialog.h Dialog::Dialog(){ } Dialog::~Dialog(){ }主窗口类MainWindow.h#ifndef MAINWINDOW_H #define MAINWINDOW_H #include QMainWindow #include dialog.h namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent 0); ~MainWindow(); private: Dialog* mpDialog; QWidget* mpShadeWindow; Ui::MainWindow *ui; private slots: void ShowAWindow(); void ResetUI(); }; #endif // MAINWINDOW_HMainWindow.cpp#include mainwindow.h #include ui_mainwindow.h MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-setupUi(this); this-resize(400, 300); mpShadeWindow new QWidget(this); QString str(QWidget{background-color:rgba(0,0,0,0.6);}); mpShadeWindow-setStyleSheet(str); mpShadeWindow-setGeometry(0, 0, 1, 1); mpShadeWindow-hide(); connect(ui-pushButton, SIGNAL(clicked(bool)), this, SLOT(ShowAWindow())); mpDialog new Dialog; mpDialog-resize(250, 150); mpDialog-setModal(true); connect(mpDialog, SIGNAL(Closed()), this, SLOT(ResetUI())); } MainWindow::~MainWindow(){ delete ui; } void MainWindow::ShowAWindow(){ mpShadeWindow-setGeometry(0, 0, this-width(), this-height()); mpShadeWindow-show(); mpDialog-show(); } void MainWindow::ResetUI(){ mpShadeWindow-hide(); mpShadeWindow-setGeometry(0,0,1,1); }工程比较简单没有注释相信这足够给小伙伴们启发思路了另外附上完整工程链接。点击打开链接我想免金币的好像最低要两个。。。。大家不要打我