首页 > 其他分享 >Qt-Qt之监听窗口改变事件(最小化、最大化、还原)

Qt-Qt之监听窗口改变事件(最小化、最大化、还原)

时间:2023-02-11 15:45:15浏览次数:49  
标签:Qt void 监听 mainwindow ui 最小化 include MainWindow

相关资料:

https://blog.csdn.net/weixin_43165135/article/details/125527497

实例代码:

.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
View Code

main.cpp

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

mainwindow.h

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

mainwindow.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("Qt之监听窗口改变事件(最小化、最大化、还原)"));
10 }
11 
12 MainWindow::~MainWindow()
13 {
14     delete ui;
15 }
16 
17 void MainWindow::changeEvent(QEvent *event)
18 {
19     if(QEvent::WindowStateChange == event->type())
20     {
21         QWindowStateChangeEvent * stateEvent = dynamic_cast<QWindowStateChangeEvent*>(event);
22         if(Q_NULLPTR != stateEvent)
23         {
24             Qt::WindowStates x = stateEvent->oldState();
25             ui->textEdit->append(QString("oldState %1").arg(x));
26             if(Qt::WindowMinimized == stateEvent->oldState())
27             {
28                 ui->textEdit->append(QString("oldState %1").arg(x));
29             }
30             else if(Qt::WindowMaximized == stateEvent->oldState())
31             {
32                 ui->textEdit->append(QString("oldState %1").arg(x));
33             }
34         }
35     }
36 }
37 
38 
39 void MainWindow::on_pushButton_clicked()
40 {
41     this->showMinimized();
42 }
43 
44 void MainWindow::on_pushButton_2_clicked()
45 {
46     this->showMaximized();
47 }
48 
49 void MainWindow::on_pushButton_3_clicked()
50 {
51     this->showNormal();
52 }
View Code

mainwindow.ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>561</width>
10     <height>273</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget">
17    <layout class="QVBoxLayout" name="verticalLayout">
18     <item>
19      <widget class="QTextEdit" name="textEdit"/>
20     </item>
21     <item>
22      <layout class="QHBoxLayout" name="horizontalLayout">
23       <item>
24        <widget class="QPushButton" name="pushButton">
25         <property name="text">
26          <string>最小化</string>
27         </property>
28        </widget>
29       </item>
30       <item>
31        <widget class="QPushButton" name="pushButton_2">
32         <property name="text">
33          <string>最大化</string>
34         </property>
35        </widget>
36       </item>
37       <item>
38        <widget class="QPushButton" name="pushButton_3">
39         <property name="text">
40          <string>还原</string>
41         </property>
42        </widget>
43       </item>
44      </layout>
45     </item>
46    </layout>
47   </widget>
48  </widget>
49  <resources/>
50  <connections/>
51 </ui>
View Code

 

 

搜索

复制

<iframe></iframe>

标签:Qt,void,监听,mainwindow,ui,最小化,include,MainWindow
From: https://www.cnblogs.com/FKdelphi/p/17111794.html

相关文章

  • Qt常用数据类型(2)
    6.日期和时间6.1.QDate//构造函数QDate::QDate();QDate::QDate(inty,intm,intd);//公共成员函数//重新设置日期对象中的日期boolQDate::setDate(intyear,intm......
  • 【大型软件开发】浅谈大型Qt软件开发(四)动态链接库的宏冲突问题、COM组件开发的常见问
    最近工作的时候有一个链接库的对接工作,在对接时发生了一些小问题,这篇FAQ是办公室写这个库的工程师戴工写的,这里记录一下:一、编译工程时报链接错误“不允许dllimport静态数......
  • mq问题排查 (队列有没有监听的人,交换机有没有绑定队列)
     发送消息  打断点能进入    监听接收消息这里打断点,发现没有消息接受  支付结果监听这里没有消费者在监听   检查交换机是否和队列已经绑定......
  • QT代码实现圆形指示灯
    1.思路:标签QLabel改变形状,根据情况显示背景色2.代码实现setLED(QLabel*label,intcolor,intsize){label->setText("");QStringmin_width=QString("min-widt......
  • Qt 动画之一:动画框架
    一、Qt动画类介绍类名功能介绍QAbstractAnimation动画基类提供基本的动画属性和接口,它有两个子类QVariantAnimation和QAnimationGroup。QAbstractAnimati......
  • Qt 动画之二:简单实例:
    一、效果二、代码Widget.h#ifndefWIDGET_H#defineWIDGET_H#include<QWidget>#include<QPushButton>#include<QPropertyAnimation>#include<QDebug>class......
  • qt 卡拉OK 歌词效果
    //思路:一、先绘制全部歌词二、设置裁剪区域三、绘制已经唱的歌词voidWidget::paintEvent(QPaintEvent*event){QLineFline(10.0,80.0,90.0,20.0);QPainter......
  • Abp vnext + MQTT
    目录Artizan.Iot.Hub.Mqtt.Application.ContractsIMqttServiceBaseIMqttConnectionService:负责MqttServer与链接相关初始化IMqttPublishingService:负责MqttServer的发......
  • 解决PyQtWebEngine安装缓慢的问题
    在使用PyQtWebEngine时候,发现pyqt5由于版本高而没有PyQtWebEngine。于是安装但是在使用清华源的时候,发现下载非常慢。我通过各种方式进行测试均需要9小时才能下载。原因未......
  • Qt-Qt实现高斯模糊效果
    相关资料:https://qa.1r1g.com/sf/ask/1044059061/实例代码:.pro1QT+=coregui23greaterThan(QT_MAJOR_VERSION,4):QT+=widgets45CONFIG+......