实现原理:
1.顶层窗体设置为无边框,背景半透明
2.顶层窗体的子窗体使用带有阴影的图片做背景
代码:
//CMainWindow.h #ifndef CMAINWINDOW_H #define CMAINWINDOW_H #includeclass CMainWindow : public QWidget { Q_OBJECT public: CMainWindow(QWidget* poParent = NULL); ~CMainWindow(); };
#endif //CMAINWINDOW_H //CMainWindow.cpp #include "CMainWindow.h"
CMainWindow::CMainWindow(QWidget *poParent) : QWidget(poParent) { this->resize(713, 500); //设置顶层窗体无边框 this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint); //设置顶层窗体背景半透明 this->setAttribute(Qt::WA_TranslucentBackground); QWidget* poMainWidget = new QWidget(this); poMainWidget->setFixedSize(713, 500); poMainWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); poMainWidget->setObjectName("poMainWidget"); //在顶层窗体子窗体上应用带有阴影的背景图片 poMainWidget->setStyleSheet("QWidget#poMainWidget { background-image : url(:/image/auth/login_bg.png); }"); }
//main.cpp #include#include "CMainWindow.h" int main(int argc, char* argv[]) { QApplication oApp(argc, argv); CMainWindow oMainWindow; oMainWindow.show(); return oApp.exec(); }
运行效果: