Qt窗口

xiaoxiao2021-02-28  114

一、一个简单的窗口

1.运行后的窗口一闪而过(数据在函数中,运行完毕就消失)

#include "mainwindow.h" #include <QApplication> void f() { MainWindow w; w.show(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); f(); return a.exec(); } 2.可以看到窗口(数据存在堆中,直到关机才消失) #include "mainwindow.h" #include <QApplication> void f() { MainWindow * w=new MainWindow(); w->show();//(*w).show(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); f(); return a.exec(); } 3.delete原有数据 #include "mainwindow.h" #include <QApplication> MainWindow * f() { MainWindow * w=new MainWindow(); w->show();//(*w).show(); return w; } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow * m = f(); int result= a.exec(); delete m; return result; }4.使一个对话框停留五秒后消失 用到了定时器(两种方法) A. 第一种 #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); startTimer(5000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::timerEvent(QTimerEvent *event) { close(); } #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; int id1; protected: void timerEvent(QTimerEvent *event); }; #endif // MAINWINDOW_H B.第二种 #include "mainwindow.h" #include <QApplication> #include <QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QTimer *timer = new QTimer; MainWindow w; QObject::connect(timer,&QTimer::timeout,&w,&MainWindow::close); timer->start(5000); w.show(); int result= a.exec(); delete timer; return result; }    
转载请注明原文地址: https://www.6miu.com/read-27306.html

最新回复(0)