新增遮罩层窗体
parent
ec7a6e1b5a
commit
2b7cba8487
|
@ -1,7 +1,4 @@
|
|||
#### 项目介绍
|
||||
Qt编写的一些开源的demo,包括串口调试助手、网络调试助手、自定义控件、各种小工具和demo等,会一直持续更新完善,代码简洁易懂,非常适合初学者学习,注释详细,每个都是独立子项目,代码随意传播使用,拒绝打赏和捐赠,欢迎各位留言评论!
|
||||
|
||||
#### 目录说明
|
||||
#### 目录说明
|
||||
| 编号 | 文件夹 | 描述 |
|
||||
| ------ | ------ | ------ |
|
||||
| 1 | lightbutton | 高亮按钮控件 |
|
||||
|
@ -22,4 +19,5 @@ Qt编写的一些开源的demo,包括串口调试助手、网络调试助手
|
|||
| 16 | pngtool | PNG图片警告去除工具 |
|
||||
| 17 | savelog | 日志重定向输出类 |
|
||||
| 18 | saveruntime | 运行时间记录类 |
|
||||
| 19 | colorwidget | 颜色拾取器 |
|
||||
| 19 | colorwidget | 颜色拾取器 |
|
||||
| 20 | maskwidget | 遮罩层窗体 |
|
|
@ -248,7 +248,7 @@ void GifWidget::initForm()
|
|||
connect(timer, SIGNAL(timeout()), this, SLOT(saveImage()));
|
||||
|
||||
this->setAttribute(Qt::WA_TranslucentBackground);
|
||||
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint);
|
||||
this->setWindowFlags(Qt::FramelessWindowHint);
|
||||
this->installEventFilter(this);
|
||||
|
||||
QStringList qss;
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmmaskwidget.h"
|
||||
#include "ui_frmmaskwidget.h"
|
||||
#include "maskwidget.h"
|
||||
#include "qdialog.h"
|
||||
|
||||
frmMaskWidget::frmMaskWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmMaskWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
MaskWidget::Instance()->setMainWidget(this);
|
||||
MaskWidget::Instance()->setDialogNames(QStringList() << "frmTest");
|
||||
}
|
||||
|
||||
frmMaskWidget::~frmMaskWidget()
|
||||
{
|
||||
delete ui;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void frmMaskWidget::on_pushButton_clicked()
|
||||
{
|
||||
QDialog d;
|
||||
d.setObjectName("frmTest");
|
||||
d.setWindowTitle("遮罩层弹出窗体");
|
||||
d.resize(400, 300);
|
||||
d.exec();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef FRMMASKWIDGET_H
|
||||
#define FRMMASKWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class frmMaskWidget;
|
||||
}
|
||||
|
||||
class frmMaskWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmMaskWidget(QWidget *parent = 0);
|
||||
~frmMaskWidget();
|
||||
|
||||
private slots:
|
||||
void on_pushButton_clicked();
|
||||
|
||||
private:
|
||||
Ui::frmMaskWidget *ui;
|
||||
};
|
||||
|
||||
#endif // FRMMASKWIDGET_H
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmMaskWidget</class>
|
||||
<widget class="QWidget" name="frmMaskWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>480</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>10</y>
|
||||
<width>131</width>
|
||||
<height>23</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>弹出遮罩窗体</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
|
@ -0,0 +1,31 @@
|
|||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmmaskwidget.h"
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setFont(QFont("Microsoft Yahei", 9));
|
||||
|
||||
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
#endif
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
QTextCodec::setCodecForCStrings(codec);
|
||||
QTextCodec::setCodecForTr(codec);
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
#endif
|
||||
|
||||
frmMaskWidget w;
|
||||
w.setWindowTitle("遮罩层窗体");
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "maskwidget.h"
|
||||
#include "qmutex.h"
|
||||
#include "qapplication.h"
|
||||
#include "qdesktopwidget.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
QScopedPointer<MaskWidget> MaskWidget::self;
|
||||
MaskWidget *MaskWidget::Instance()
|
||||
{
|
||||
if (self.isNull()) {
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
if (self.isNull()) {
|
||||
self.reset(new MaskWidget);
|
||||
}
|
||||
}
|
||||
|
||||
return self.data();
|
||||
}
|
||||
|
||||
MaskWidget::MaskWidget(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
mainWidget = 0;
|
||||
setOpacity(0.7);
|
||||
setBgColor(QColor(0, 0, 0));
|
||||
|
||||
//不设置主窗体则遮罩层大小为默认桌面大小
|
||||
this->setGeometry(qApp->desktop()->geometry());
|
||||
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
|
||||
|
||||
//绑定全局事件,过滤弹窗窗体进行处理
|
||||
qApp->installEventFilter(this);
|
||||
}
|
||||
|
||||
void MaskWidget::setMainWidget(QWidget *mainWidget)
|
||||
{
|
||||
if (this->mainWidget != mainWidget) {
|
||||
this->mainWidget = mainWidget;
|
||||
}
|
||||
}
|
||||
|
||||
void MaskWidget::setDialogNames(const QStringList &dialogNames)
|
||||
{
|
||||
if (this->dialogNames != dialogNames) {
|
||||
this->dialogNames = dialogNames;
|
||||
}
|
||||
}
|
||||
|
||||
void MaskWidget::setOpacity(double opacity)
|
||||
{
|
||||
this->setWindowOpacity(opacity);
|
||||
}
|
||||
|
||||
void MaskWidget::setBgColor(const QColor &bgColor)
|
||||
{
|
||||
QPalette palette = this->palette();
|
||||
palette.setBrush(QPalette::Background, bgColor);
|
||||
this->setPalette(palette);
|
||||
}
|
||||
|
||||
void MaskWidget::showEvent(QShowEvent *)
|
||||
{
|
||||
if (mainWidget != 0) {
|
||||
this->setGeometry(mainWidget->geometry());
|
||||
}
|
||||
}
|
||||
|
||||
bool MaskWidget::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Show) {
|
||||
if (dialogNames.contains(obj->objectName())) {
|
||||
this->show();
|
||||
this->activateWindow();
|
||||
QWidget *w = (QWidget *)obj;
|
||||
w->activateWindow();
|
||||
}
|
||||
} else if (event->type() == QEvent::Hide) {
|
||||
if (dialogNames.contains(obj->objectName())) {
|
||||
this->hide();
|
||||
}
|
||||
} else if (event->type() == QEvent::WindowActivate) {
|
||||
//当主窗体激活时,同时激活遮罩层
|
||||
if (mainWidget != 0) {
|
||||
if (obj->objectName() == mainWidget->objectName()) {
|
||||
if (this->isVisible()) {
|
||||
this->activateWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
#ifndef MASKWIDGET_H
|
||||
#define MASKWIDGET_H
|
||||
|
||||
/**
|
||||
* 弹窗遮罩层控件 作者:feiyangqingyun(QQ:517216493) 2016-12-26
|
||||
* 1:可设置需要遮罩的主窗体,自动跟随主窗体位置显示遮罩面积
|
||||
* 2:只需要将弹窗窗体的名称一开始传入队列即可,足够简单
|
||||
* 3:可设置透明度
|
||||
* 4:可设置遮罩层颜色
|
||||
* 5:不阻塞消息循坏
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#ifdef quc
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
|
||||
#include <QtDesigner/QDesignerExportWidget>
|
||||
#else
|
||||
#include <QtUiPlugin/QDesignerExportWidget>
|
||||
#endif
|
||||
|
||||
class QDESIGNER_WIDGET_EXPORT MaskWidget : public QWidget
|
||||
#else
|
||||
class MaskWidget : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static MaskWidget *Instance();
|
||||
explicit MaskWidget(QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent *);
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
|
||||
private:
|
||||
static QScopedPointer<MaskWidget> self;
|
||||
QWidget *mainWidget; //需要遮罩的主窗体
|
||||
QStringList dialogNames; //可能弹窗的窗体对象名称集合链表
|
||||
|
||||
public Q_SLOTS:
|
||||
void setMainWidget(QWidget *mainWidget);
|
||||
void setDialogNames(const QStringList &dialogNames);
|
||||
void setBgColor(const QColor &bgColor);
|
||||
void setOpacity(double opacity);
|
||||
};
|
||||
|
||||
#endif // MASKWIDGET_H
|
|
@ -0,0 +1,25 @@
|
|||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2017-01-05T22:11:54
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = maskwidget
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
SOURCES += maskwidget.cpp
|
||||
SOURCES += frmmaskwidget.cpp
|
||||
|
||||
HEADERS += maskwidget.h
|
||||
HEADERS += frmmaskwidget.h
|
||||
|
||||
FORMS += frmmaskwidget.ui
|
||||
|
||||
|
Loading…
Reference in New Issue