diff --git a/README.md b/README.md index 47b87eb..a12e7f9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -#### 项目介绍 -Qt编写的一些开源的demo,包括串口调试助手、网络调试助手、自定义控件、各种小工具和demo等,会一直持续更新完善,代码简洁易懂,非常适合初学者学习,注释详细,每个都是独立子项目,代码随意传播使用,拒绝打赏和捐赠,欢迎各位留言评论! - -#### 目录说明 +#### 目录说明 | 编号 | 文件夹 | 描述 | | ------ | ------ | ------ | | 1 | lightbutton | 高亮按钮控件 | @@ -22,4 +19,5 @@ Qt编写的一些开源的demo,包括串口调试助手、网络调试助手 | 16 | pngtool | PNG图片警告去除工具 | | 17 | savelog | 日志重定向输出类 | | 18 | saveruntime | 运行时间记录类 | -| 19 | colorwidget | 颜色拾取器 | \ No newline at end of file +| 19 | colorwidget | 颜色拾取器 | +| 20 | maskwidget | 遮罩层窗体 | \ No newline at end of file diff --git a/gifwidget/gifwidget.cpp b/gifwidget/gifwidget.cpp index 84ed18b..069fa89 100644 --- a/gifwidget/gifwidget.cpp +++ b/gifwidget/gifwidget.cpp @@ -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; diff --git a/maskwidget/frmmaskwidget.cpp b/maskwidget/frmmaskwidget.cpp new file mode 100644 index 0000000..60b97a2 --- /dev/null +++ b/maskwidget/frmmaskwidget.cpp @@ -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(); +} diff --git a/maskwidget/frmmaskwidget.h b/maskwidget/frmmaskwidget.h new file mode 100644 index 0000000..5e17fa5 --- /dev/null +++ b/maskwidget/frmmaskwidget.h @@ -0,0 +1,25 @@ +#ifndef FRMMASKWIDGET_H +#define FRMMASKWIDGET_H + +#include + +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 diff --git a/maskwidget/frmmaskwidget.ui b/maskwidget/frmmaskwidget.ui new file mode 100644 index 0000000..727df8e --- /dev/null +++ b/maskwidget/frmmaskwidget.ui @@ -0,0 +1,32 @@ + + + frmMaskWidget + + + + 0 + 0 + 600 + 480 + + + + Form + + + + + 10 + 10 + 131 + 23 + + + + 弹出遮罩窗体 + + + + + + diff --git a/maskwidget/main.cpp b/maskwidget/main.cpp new file mode 100644 index 0000000..b7b9fdd --- /dev/null +++ b/maskwidget/main.cpp @@ -0,0 +1,31 @@ +#pragma execution_character_set("utf-8") + +#include "frmmaskwidget.h" +#include +#include + +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(); +} diff --git a/maskwidget/maskwidget.cpp b/maskwidget/maskwidget.cpp new file mode 100644 index 0000000..ca1a68c --- /dev/null +++ b/maskwidget/maskwidget.cpp @@ -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::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); +} diff --git a/maskwidget/maskwidget.h b/maskwidget/maskwidget.h new file mode 100644 index 0000000..f3cc0ec --- /dev/null +++ b/maskwidget/maskwidget.h @@ -0,0 +1,49 @@ +#ifndef MASKWIDGET_H +#define MASKWIDGET_H + +/** + * 弹窗遮罩层控件 作者:feiyangqingyun(QQ:517216493) 2016-12-26 + * 1:可设置需要遮罩的主窗体,自动跟随主窗体位置显示遮罩面积 + * 2:只需要将弹窗窗体的名称一开始传入队列即可,足够简单 + * 3:可设置透明度 + * 4:可设置遮罩层颜色 + * 5:不阻塞消息循坏 + */ + +#include + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#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 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 diff --git a/maskwidget/maskwidget.pro b/maskwidget/maskwidget.pro new file mode 100644 index 0000000..60f1992 --- /dev/null +++ b/maskwidget/maskwidget.pro @@ -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 + +