Qt console "WARNING: QApplication was not created in the main () thread"

I am creating a very simple C ++ QT console application from the example given here when the stack overflows.

How to use QFileSystemWatcher to track folder changes

The code exactly matches the code in this application, and I am developing with Qt UI, Qt Creator with MinGW 32bit. I chose the console application from the projects that I could choose, since I do not need a graphical user interface. After the application has finished loading, the application displays the error message "WARNING: QApplication was not created in the main () thread" and then does nothing.

I tried to debug the application, but don't get breakpoint hits, I don't think that debugging works in the editor.

I quickly switched and changed QApplication to QCoreApplication when I develop a console application, but get the 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 is as follows:

 QT += core QT += widgets QT -= gui TARGET = fsw CONFIG += console CONFIG -= app_bundle TEMPLATE = app HEADERS += fileSystemReceiver.h SOURCES += \ main.cpp 
+1
source share
1 answer

There were several problems in your project:

  • QCoreApplication in a program that should show QWidget

  • Call the main file main.cpp main.moc. This indicates that you do not quite understand how moc works and what it means.

  • cout in a Qt program, unlike QTextStream or qDebug ().

  • Q_FOREACH in source code not used by another application, and therefore a collision cannot occur. You should just use "foreach".

  • You do not use the const reference for the string when iterating with foreach, although you seem to be reading it, not changing it.

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

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

  • You add "the kernel to the CONFIG variable when it is by default."

  • You include #include <QtWidgets/QFoo> instead of #include <QFoo> to preserve #include <QFoo> with Qt 4, and with an explicit include include construct in general.

  • You add CONFIG += console for an application without a console.

  • You add CONFIG -= app_bundle for an application that does not support the console.

  • You are using backslashes SOURCES, but not for HEADERS. This is incompatible.

  • You create an instance of MyClass on the heap as opposed to the stack, to make it simpler for you, since it is already adequately protected by the event loop to remain valid for the intended area.

Among other things, your problem seems to be related to qDebug () based on a discussion of comments. You should follow the document below on how to properly configure QtCreator for debugging.

Debugger setup

+2
source

Source: https://habr.com/ru/post/981054/


All Articles