“必须在QWidget之前构造QApplication” [英] "Must construct a QApplication before a QWidget"
问题描述
QFileDialog :: getSaveFileName()
但是当我在下面评论行程序运行成功。 my MainWindow.h class MainWindow:public QMainWindow
{
Q_OBJECT
public:
QString fileName();
};
在MainWindow.cpp上定义:
QString MainWindow :: fileName(){
returnF:/ Users / Admin / Desktop /+ dnldName;
}
执行fileName();
void Dialog :: on_browseButton_clicked()
{
QFileDialog folder;
folder.setFileMode(QFileDialog :: Directory);
folder.setOption(QFileDialog :: ShowDirsOnly);
dirPath = folder.getSaveFileName(this,tr(Save File),mWinObj.fileName(),tr(All Files));
ui-> savePathEdit-> setText(dirPath);
}
程序生成成功,但在尝试运行时会出现以下错误。
开始F:\Users\Admin\Desktop\Imp Docs\C ++ apps\build -GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe ...
QWidget:必须在QWidget之前构造QApplication
传递给C运行时函数的无效参数。
传递给C运行时函数的参数无效。
F:\Users\Admin\Desktop\Imp Docs\C ++ apps\build-GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe以代码3退出
我的Main.cpp
#includemainwindow.h
#include< QApplication>
int main(int argc,char * argv [])
{
QApplication a(argc,argv);
MainWindow w;
w.show();
return a.exec();
}
代码有什么问题,我该如何解决?
您不能将小部件创建为全局对象,因为这些对象将在main函数中的应用程序对象之前创建。
在QApplication对象构造之后在主函数中创建窗口小部件,或者保留一个全局指针指向窗口小部件,如果想要全局访问这不是一个好的编程风格)。
I'm trying to make a simple Qt program.I have successfully build and run the program several times.These errors occurred when I added a getter method to pass a string from my MainWindow to a dialog's QFileDialog::getSaveFileName()
But when I comment below lines the programs runs successfully. my MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
QString fileName();
};
defination on MainWindow.cpp:
QString MainWindow::fileName() {
return "F:/Users/Admin/Desktop/" + dnldName;
}
Implementation of fileName();
void Dialog::on_browseButton_clicked()
{
QFileDialog folder;
folder.setFileMode(QFileDialog::Directory);
folder.setOption(QFileDialog::ShowDirsOnly);
dirPath = folder.getSaveFileName(this, tr("Save File"), mWinObj.fileName(), tr("All Files"));
ui->savePathEdit->setText(dirPath);
}
The program builds successfully but gives the following errors when I try to run it.
Starting F:\Users\Admin\Desktop\Imp Docs\C++ apps\build-GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe...
QWidget: Must construct a QApplication before a QWidget
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
F:\Users\Admin\Desktop\Imp Docs\C++ apps\build-GUINetworkApp-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\GUINetworkApp.exe exited with code 3
My Main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
What's wrong with the code and how can I fix it?
You cannot create widgets as global objects since those objects will be created before the application object in the main function. Then your error will happen.
Create your widget within the main function after the QApplication object construction, or just keep a global pointer to your widget if you want global access (but which is not a good programming style).
这篇关于“必须在QWidget之前构造QApplication”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!