Qt-鼠标点击别处隐藏widget

相关资料:

https://download.csdn.net/download/zhujianqiangqq/13131211     代码包下载

 

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
32 
33 RESOURCES +=
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 #include <QFile>
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     QApplication a(argc, argv);
 9     MainWindow w;
10     w.show();
11     return a.exec();
12 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QTabWidget>
 6 #include <QLineEdit>
 7 #include <QDebug>
 8 #include <QMouseEvent>
 9 
10 QT_BEGIN_NAMESPACE
11 namespace Ui { class MainWindow; }
12 QT_END_NAMESPACE
13 
14 class MainWindow : public QMainWindow
15 {
16     Q_OBJECT
17 
18 public:
19     MainWindow(QWidget *parent = nullptr);
20     ~MainWindow();
21 protected:
22     bool eventFilter(QObject *obj, QEvent *event);
23 
24 private slots:
25     void on_pushButton_clicked();
26 
27 private:
28     Ui::MainWindow *ui;
29 
30     QWidget *m_pTabCalibration;
31 };
32 #endif // MAINWINDOW_H
View Code

mainwinodw.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     setWindowTitle(QStringLiteral("点击别处隐藏Widget"));
10 
11     // 注册事件过滤
12     installEventFilter(this);
13 
14     // 内容显示
15     m_pTabCalibration = new QWidget(this);
16     m_pTabCalibration->setGeometry(0, 0, 200, 200);
17 
18     QLineEdit *oEdit = new QLineEdit(m_pTabCalibration);
19     oEdit->setGeometry(10, 10, 100, 30);
20 
21     QLineEdit *oEdit2 = new QLineEdit(m_pTabCalibration);
22     oEdit2->setGeometry(10, 40, 100, 30);
23 
24     QLineEdit *oEdit3 = new QLineEdit(m_pTabCalibration);
25     oEdit3->setGeometry(10, 60, 100, 30);
26 }
27 
28 MainWindow::~MainWindow()
29 {
30     delete ui;
31 }
32 
33 // 事件过滤,这里是重点
34 bool MainWindow::eventFilter(QObject *obj, QEvent *event)
35 {
36     // WindowDeactivate事件是鼠标点击了本程序以外的地方。如需其他事件请查看QEvent常量。
37     if (QEvent::MouseButtonPress == event->type())
38     {
39         if(!m_pTabCalibration->geometry().contains(this->mapFromGlobal(QCursor::pos())))
40         {
41             m_pTabCalibration->setVisible(false);
42         };
43     }
44     return QObject::eventFilter(obj, event);
45 }
46 
47 
48 void MainWindow::on_pushButton_clicked()
49 {
50     m_pTabCalibration->setVisible(true);
51 }
View Code

 

posted on 2020-11-20 20:00  疯狂delphi  阅读(632)  评论(0编辑  收藏  举报

导航