I'm creating a very simple C++ QT console application from an example given here on stack overflow.
How to use QFileSystemWatcher to monitor a folder for change
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 removegui
.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