Qt控制台应用程序“警告:QApplication没有在main()线程中创建” [英] Qt console application "WARNING: QApplication was not created in the main() thread"

查看:6586
本文介绍了Qt控制台应用程序“警告:QApplication没有在main()线程中创建”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何QFileSystemWatcher



代码与该应用程序中的代码一样, Qt的UI,Qt Creator与MinGW 32位。我从我可以选择的项目中选择控制台应用程序,因为我不需要图形用户界面。一旦应用程序加载完成,应用程序显示错误消息警告:QApplication没有在main()线程中创建,然后什么都不做。



应用程序但没有得到断点命中,我不认为调试是在编辑器中工作。



我有一个快速去,并将QApplication更改为QCoreApplication被视为I



filesystemreceiver.h

 

code> #ifndef FILESYSTEMRECEIVER_H
#define FILESYSTEMRECEIVER_H

#include< iostream>

using namespace std;

#include< QtCore / QApplication>
#include< QtCore / QFileSystemWatcher>
#include< QtCore / QDebug>
#include< QtWidgets / QWidget>
#include< QtWidgets / QMessageBox>

class MyClass:public QWidget
{
Q_OBJECT
public:
MyClass(QWidget * parent = 0)
:QWidget {}

〜MyClass(){}
public slots:
void showModified(const QString& str)
{
Q_UNUSED(str)
cout<< 收到一条消息! << endl;
// QMessageBox :: information(this,Directory Modified,Your Directory is modified);
}
};

#endif // FILESYSTEMRECEIVER_H

main.cpp

  #include< iostream> 

using namespace std;

#include< QtCore / QApplication>
#include< QtCore / QFileSystemWatcher>
#include< QtCore / QDebug>
#include< QtWidgets / QWidget>
#include< QtWidgets / QMessageBox>

#includefileSystemReceiver.h

int main(int argc,char * argv [])
{
QApplication app(argc,argv );
QFileSystemWatcher watcher;
watcher.addPath(C:/ QtTest);

QStringList directoryList = watcher.directories();
Q_FOREACH(QString目录,directoryList)
qDebug()< 目录名<<目录<<\ n;

MyClass * mc = new MyClass;

QObject :: connect(& watcher,SIGNAL(directoryChanged(QString)),mc,SLOT(showModified(QString)

return app.exec();
}

我的pro文件看起来像这样:

  QT + = core 
QT + = widgets

QT - = gui

TARGET = fsw
CONFIG + = console
CONFIG - = app_bundle

TEMPLATE = app

HEADERS + = fileSystemReceiver.h

SOURCES + = \
main.cpp


解决方案

您的项目中存在以下问题:




  • QCoreApplication应该显示一个QWidget


  • 调用main.cpp源文件main.moc。这表示你不太明白moc是如何工作的以及它是什么。


  • cout在Qt程序中,而不是QTextStream或qDebug / p>


  • Q_FOREACH在源代码中不被其他应用程序重用,因此通常不会发生冲突。你应该使用foreach。


  • 你不是使用const引用的字符串迭代与foreach即使你似乎只是阅读,


  • 这里有硬编码路径,而不是一个很好分开的地方的const字符串: watcher.addPath :/ QtTest);


  • 您正在添加 widgets CONFIG变量,但您删除 gui



  • 您包含 #include< QtWidgets / QFoo> code> #include< QFoo> 以保留使用Qt 4构建的选项,并且通常包含清楚的构建系统包含路径。


  • 您正在为基于非控制台的应用程序添加 CONFIG + = console


  • 您正在为基于非控制台的应用程序添加 CONFIG - = app_bundle


  • SOURCES变量的反斜杠,而不是HEADERS的反斜杠。这是不一致的。


  • 您在堆上创建一个MyClass实例,而不是堆栈,使它更简单,因为它已经被事件正确保护




最重要的是,你的问题似乎是与qDebug )基于评论讨论。您应该按照下面的文档,如何正确设置QtCreator进行调试。



设置调试器<​​/a>


I'm creating a very simple C++ QT console application from an example given here on stack overflow.

How to QFileSystemWatcher

The code is exactly as the code in that application and I'm developing with Qt's UI, Qt Creator with MinGW 32bit. I selected the console application from the projects I could choose as I have no need for a graphical user interface. Once the application has finished loading, the application shows the error message "WARNING: QApplication was not created in the main() thread" then does nothing.

I have tried debugging the application but get no breakpoints hit, I don't think debugging is working in the editor.

I had a quick go and changed the QApplication to a QCoreApplication seen as I am developing a console application but get the exact same error message.

filesystemreceiver.h

#ifndef FILESYSTEMRECEIVER_H
#define FILESYSTEMRECEIVER_H

#include <iostream>

using namespace std;

#include <QtCore/QApplication>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QDebug>
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>

class MyClass : public QWidget
{
    Q_OBJECT
public:
    MyClass(QWidget* parent=0)
        :QWidget(parent){}

    ~MyClass() {}
public slots:
    void showModified(const QString& str)
    {
        Q_UNUSED(str)
        cout << "A message has been received!" << endl;
        //QMessageBox::information(this,"Directory Modified", "Your Directory is modified");
    }
};

#endif // FILESYSTEMRECEIVER_H

main.cpp

#include <iostream>

using namespace std;

#include <QtCore/QApplication>
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QDebug>
#include <QtWidgets/QWidget>
#include <QtWidgets/QMessageBox>

#include "fileSystemReceiver.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QFileSystemWatcher watcher;
    watcher.addPath("C:/QtTest");

    QStringList directoryList = watcher.directories();
    Q_FOREACH(QString directory, directoryList)
              qDebug() << "Directory name" << directory <<"\n";

    MyClass* mc = new MyClass;

    QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));

    return app.exec();
}

My pro file looks like this:

QT       += core
QT       += widgets

QT       -= gui

TARGET   =  fsw
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

HEADERS += fileSystemReceiver.h

SOURCES  += \
    main.cpp

解决方案

You have had several issues ongoing in your project:

  • QCoreApplication in a program that is supposed to show a QWidget

  • Calling the main.cpp source file main.moc. That indicates that you do not quite understand how moc works and what it is about.

  • cout in a Qt program as opposed to QTextStream or qDebug().

  • Q_FOREACH in a source code not reused by other application, and hence no collision could normally occur. You should use "foreach" simply.

  • You are not using const reference for the string while iterating with the foreach even though you seem to be only reading it, not modifying.

  • You have hard coded path here instead of a const string in a well separated place: watcher.addPath("C:/QtTest");

  • You are adding widgets to the CONFIG variable, but you remove gui.

  • You are adding `core to the CONFIG variable when that is in there by default.

  • You include #include <QtWidgets/QFoo> instead of #include <QFoo> to keep the option of building with Qt 4, and in general with clearly buildsystem include paths.

  • You are adding CONFIG += console for a non-console based application.

  • You are adding CONFIG -= app_bundle for a non-console based application.

  • You are using back-slash for the SOURCES variable, but not for the HEADERS. This is inconsitent.

  • You create a MyClass instance on the heap as opposed to the stack to make it simpler for you as it is already properly guarded by the event loop to remain valid for the intended scope.

On top of all that, your issue seems to be with qDebug() based on the comment discussion. You should follow the document below how to set up QtCreator for debugging properly.

Setting Up Debugger

这篇关于Qt控制台应用程序“警告:QApplication没有在main()线程中创建”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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