如何在 Main() 之外创建和显示 MainWindow?
How to create and display the MainWindow outside the Main()?
我在网上进行了长时间的研究,但找不到任何明确的信息。
我认为答案很明显,但我是 Qt 的初学者。
为什么这段代码不起作用?我的 windows 弹出速度非常快。
Main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test test;
return a.exec();
}
Test.cpp
#include "test.h"
Test::Test()
{
MainWindow w;
w.show();
}
这一项工作(window 保持开放):
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
谢谢!
window 关闭,因为它是 Test 构造函数的局部变量,当构造函数退出时,将调用其析构函数,从而将其关闭。您需要使 window 对象成为 Test class.
的成员变量
我在网上进行了长时间的研究,但找不到任何明确的信息。 我认为答案很明显,但我是 Qt 的初学者。 为什么这段代码不起作用?我的 windows 弹出速度非常快。
Main.cpp
#include <QApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Test test;
return a.exec();
}
Test.cpp
#include "test.h"
Test::Test()
{
MainWindow w;
w.show();
}
这一项工作(window 保持开放):
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
谢谢!
window 关闭,因为它是 Test 构造函数的局部变量,当构造函数退出时,将调用其析构函数,从而将其关闭。您需要使 window 对象成为 Test class.
的成员变量