Qt-Qt之使用QEventLoop说明

相关资料:

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

实例代码:

.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 <QTimer>
 6 #include <QEventLoop>
 7 #include <QDialog>
 8 #include <windows.h>
 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 
22 private slots:
23     void on_pushButton_clicked();
24 
25     void on_pushButton_2_clicked();
26 
27     void on_pushButton_3_clicked();
28 
29     void on_pushButton_4_clicked();
30 
31     void on_pushButton_5_clicked();
32 
33     void on_pushButton_6_clicked();
34 
35 private:
36     Ui::MainWindow *ui;
37     QEventLoop loop;//定义一个事件循环
38 };
39 #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 
10     setWindowTitle(QStringLiteral("Qt之使用QEventLoop说明"));
11 }
12 
13 MainWindow::~MainWindow()
14 {
15     delete ui;
16 }
17 
18 void MainWindow::on_pushButton_clicked()
19 {
20     loop.exec();
21     ui->textEdit->append(QStringLiteral("loop exec OK!"));
22 }
23 
24 void MainWindow::on_pushButton_2_clicked()
25 {
26     loop.quit();
27     ui->textEdit->append(QStringLiteral("loop quit OK!"));
28 }
29 
30 void MainWindow::on_pushButton_3_clicked()
31 {
32     ui->textEdit->append(QStringLiteral("Sleep 1 OK!"));
33     Sleep(3000);
34     ui->textEdit->append(QStringLiteral("Sleep 3000 OK!"));
35 }
36 
37 void MainWindow::on_pushButton_4_clicked()
38 {
39     ui->textEdit->append(QStringLiteral("QEventLoop 1 OK!"));
40     QEventLoop eventloop;
41     QTimer::singleShot(3000, &eventloop, SLOT(quit()));
42     eventloop.exec();
43     ui->textEdit->append(QStringLiteral("QEventLoop 3000 OK!"));
44 }
45 
46 void MainWindow::on_pushButton_5_clicked()
47 {
48     QDialog dlg;
49     dlg.show();
50     ui->textEdit->append(QStringLiteral("QDialog OK!"));
51 }
52 
53 void MainWindow::on_pushButton_6_clicked()
54 {
55     QDialog dlg;
56     dlg.show();
57     ui->textEdit->append(QStringLiteral("QDialog QEventLoop OK!"));
58     QEventLoop loop;
59     connect(&dlg, SIGNAL(finished(int)), &loop, SLOT(quit()));
60     loop.exec(QEventLoop::ExcludeUserInputEvents);
61 }
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>699</width>
10     <height>221</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_2">
18     <item>
19      <layout class="QHBoxLayout" name="horizontalLayout" stretch="2,3">
20       <item>
21        <widget class="QTextEdit" name="textEdit"/>
22       </item>
23       <item>
24        <layout class="QVBoxLayout" name="verticalLayout">
25         <item>
26          <layout class="QHBoxLayout" name="horizontalLayout_3">
27           <item>
28            <widget class="QPushButton" name="pushButton_5">
29             <property name="text">
30              <string>QDialog 直接弹</string>
31             </property>
32            </widget>
33           </item>
34           <item>
35            <widget class="QPushButton" name="pushButton_6">
36             <property name="text">
37              <string>QDialog QEventLoop</string>
38             </property>
39            </widget>
40           </item>
41          </layout>
42         </item>
43         <item>
44          <layout class="QHBoxLayout" name="horizontalLayout_2">
45           <item>
46            <widget class="QPushButton" name="pushButton">
47             <property name="text">
48              <string>loop.exec()</string>
49             </property>
50            </widget>
51           </item>
52           <item>
53            <widget class="QPushButton" name="pushButton_2">
54             <property name="text">
55              <string>loop.quit()</string>
56             </property>
57            </widget>
58           </item>
59          </layout>
60         </item>
61         <item>
62          <layout class="QHBoxLayout" name="horizontalLayout_4">
63           <item>
64            <widget class="QPushButton" name="pushButton_3">
65             <property name="text">
66              <string>Sleep(3000)</string>
67             </property>
68            </widget>
69           </item>
70           <item>
71            <widget class="QPushButton" name="pushButton_4">
72             <property name="text">
73              <string>QEventLoop(3000)</string>
74             </property>
75            </widget>
76           </item>
77          </layout>
78         </item>
79        </layout>
80       </item>
81      </layout>
82     </item>
83    </layout>
84   </widget>
85  </widget>
86  <resources/>
87  <connections/>
88 </ui>
View Code

 

 

 

 

 

posted on 2022-09-29 17:19  疯狂delphi  阅读(81)  评论(0编辑  收藏  举报

导航