1

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
8
  • It is unclear what you are asking because there is no code Jan 12, 2014 at 17:21
  • The code is in the link.
    – ceorron
    Jan 12, 2014 at 17:23
  • One non-obvious thing about Qt is that you need to create a QApplication object before doing anything else, even if you don't access this object directly. It sounds like that's what's missing from your code.
    – laurent
    Jan 12, 2014 at 17:24
  • That's what the code apparently does, I'll try and make the QApplication global and see if that makes a difference. - Nope
    – ceorron
    Jan 12, 2014 at 17:27
  • Questions need to stand alone. Put your code here and remove the link. Jan 12, 2014 at 17:42

1 Answer 1

2

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

5
  • 1
    Note Laszlo this is a place for answers to the question not a critique of the code. Please keep in mind that the code is a modification of the code in the link. I would suggest that you look there if you have a lot of problems thanks, for the help anyway.
    – ceorron
    Jan 13, 2014 at 15:39
  • 2
    @ceorron: if you paste code here, expect that certain people will devote their free time to help you with this as much as possible. My answer does cover the solution, too, with further rooms for improvement. Please do not take it personal or that negatively. We are here for improving things, aren't we? :-) Jan 13, 2014 at 15:44
  • Yes we are here to improve things and it is great that you would take the time and I will me making those changes. Thank you. I just wanted to say that in general stack overflow is for answers not critiques in general.
    – ceorron
    Jan 13, 2014 at 15:51
  • 2
    @ceorron: this is my answer. Clean up your code while fixing the issue because then it will all work. Without a clean software, it is lotta harder to detect these issues. Jan 13, 2014 at 15:54
  • +1 to Laszlo for comprehensive code critique. There's a lot of helpful stuff in there.
    – donturner
    Feb 10, 2014 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.