“必须在QWidget之前构造QApplication” [英] "Must construct a QApplication before a QWidget"

查看:1520
本文介绍了“必须在QWidget之前构造QApplication”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的Qt程序。我已经成功地构建和运行程序几次。这些错误发生时,我添加了一个getter方法将一个字符串从我的MainWindow传递到对话框的 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆