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 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
Related
In an attempt to incorporate a windows platform feature into an otherwise crossplatform application, I've made a one-function VC++ DLL in visual studio that uses Windows.Management.Deployment.PackageManager to get some details on all installed windows store apps.
The function works fine as a standalone application, and I can successfully build the DLL with MSVC that links properly with my MinGW main application (I can happily return primitive data from the dll, etc) - but any attempt to execute a function from the dll containing code relating to PackageManager crashes my application in runtime with the unhelpful code -529697949.
Here's some minimal code blocks that replicate:
main.cpp in the main application:
#include <QCoreApplication>
#include "mylib/WindowsAppsLib.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto hi = (Helpers::sayHi());
qDebug() << (hi);
return a.exec();
}
dll header:
#pragma once
#define WINDOWSAPPSLIB_API __declspec(dllexport)
namespace Helpers
{
extern "C" WINDOWSAPPSLIB_API const char* sayHi();
}
dll source:
#include "stdafx.h"
#include <sddl.h>
#include <collection.h>
#include "WindowsAppsLib.h"
#include <windows.h>
#using <Windows.winmd>
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace std;
const char* Helpers::sayHi()
{
auto packageManager = ref new Windows::Management::Deployment::PackageManager();
auto packages = packageManager->FindPackages();
return "Hi!";
}
Without the two lines relating to packagemanger, the program runs fine and prints "Hi!". When included, the program crashes with code -529697949 as soon as sayHi() is called. The two lines in themselves have their dependencies available and don't cause exceptions.
Any clues on how I might proceed to investigate this? Nothing I've been able to get out of this system is getting me closer to identifying the problem. Is this the sensible way to access Windows.Management.Deployment.PackageManager from within a plain C++ MinGW application to begin with?
I am using Qt5 and I found this example code , but I need to show video in some of available Qt visual objects, in which objects I can use this video overview code with VideoWidget? I am Qt beginner, and I am trying to understand how Qt works.
Thanks!
In C++, a QVideoWidget inherits from QWidget, you can put in a window or in another widget.
There's an example "videowidget" project in the Examples directory of the Qt sources.
A minimal exemple:
main.cpp
#include <QApplication>
#include <QMainWindow>
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QMediaPlaylist>
int main(int argc, char * argv[])
{
QApplication testApp(argc, argv);
QMainWindow w;
QVideoWidget videoWidget(&w);
w.setCentralWidget(&videoWidget);
QMediaPlayer *player = new QMediaPlayer(&w);
player->setMedia( QUrl::fromLocalFile("E:\\big_buck_bunny.mp4") );
player->setVideoOutput(&videoWidget);
w.show();
player->play();
return testApp.exec();
}
test_video.pro:
QT += core gui widgets multimedia multimediawidgets
TARGET = test_video
TEMPLATE = app
SOURCES += main.cpp
In QT creator, I am developing a program in QT and need to include the following files:
QPtrList.h
QPtrQueue.h
QString.h
However these files do not seem to be present and I am getting this error:
Expected temeplate-name before '<' token
Is there anyway to add these files to my installation of QT?
Use QT4.8 or QT5 (5 is better)
QPtrList.h and QPtrQueue.h are not supported by QT4.8 and QT5.
Use #include <QList> instead of QPtrList.h
Use #include <QQueue> instead of QPtrQueue.h
Example "console" in QT5:
#include <QList>
#include <QQueue>
#include <QString>
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
Pro file:
QT += core
QT -= gui
TARGET = untitled8
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
look i'm kind of new to programming in c++ with Qt so i've downloaded the 5.0 version on my win 7 64 bit. i have made a class named Mafenetre and i implemented its code and it seems perfect but when i i try to run the main program it says
C:\Users\Zbart3i\Downloads\Programs\test\main.cpp:3: error: C1083: Cannot open include file: 'Mafenetre.h': No such file or directory
this is my pro code:
SOURCES += \
main.cpp \
mafenetre.cpp
QT+=widgets
HEADERS += \
mafenetre.h
this is code of Mafenetre.h:
#ifndef MAFENETRE_H
#define MAFENETRE_H
include < QtWidgets>
class Mafenetre:public QWidget
{
public:
Mafenetre();
private:
QPushButton *m_bouton;
};
#endif // MAFENETRE_H
this is Mafenetre's.cpp:
#include "mafenetre.h"
Mafenetre::Mafenetre():QWidget()
{
setFixedSize(300,150);
m_bouton=new QPushButton("pimp mon bouton",this);
m_bouton->setFont(QFont("monotype corsiva",15));
m_bouton->setCursor(Qt::PointingHandCursor);
}
and the main.cpp
#include< QtWidgets/QApplication>
#include< QtWidgets>
#include< Mafenetre.h>
void main(int argc, char *argv[])
{
QApplication app(argc, argv);
Mafenetre fenetre;
fenetre.show();
app.exec();
}
In C++, you should use the #include "name", not #include (Quote marks instead of triangle brackets), as they have different meanings.
#include looks in certain directories where libraries (including the standard library) is installed.
#include "name" looks first in your project's folders, and then checks the other directories.
Well, to be technically accurate, what folders they look in, and in one order, is compiler-specific.
This:
#include <Mafenetre.h>
Should be this:
#include "Mafenetre.h"
Read this:
MinGW #include search directories
Other Directories Searched by Default
The minimal list of directories, identified as described
above, specifies the only locations which will be searched by default,
for system header files or for headers associated with user installed
libraries; however, there is one exception. Astute readers may have
noticed that the include file search path is itemised in a pair of
sequential lists, with the second being concatenated to the first;
however, the first, identified as the #include "..." search list
appears to be empty. In reality, this apparent emptiness is possibly
misleading; unless the user specifies the "-I-" option when invoking
GCC, this list contains exactly one directory: the directory in which
the source file containing the #include "file" directive resides.
Notice that this one additional directory is not searched for headers specified with the #include <file> form of the #include directive; it applies only for those headers specified using the #include "file" form of the directive.
And make sure your "Mafenetre.h" file is in the same folder as your main.cpp file. Otherwise you need to do #include "folderPath/fileName.h", and also add the path to the SOURCES or HEADERS variables in the .pro file.
Also make sure the spelling is identical, and that the proper case is used - sometimes it matters, sometimes it doesn't.
If you are still having trouble, try to compile a simple project consisting of just a single main.cpp, to make sure everything is installed correctly.
Are you sure you have included the right header. Also i don't see you using the QObject macro in your header file. It should be something like this:
Mafenetre.h:
#ifndef MAFENETRE_H
#define MAFENETRE_H
#include <QWidget>
class Mafenetre : public QWidget
{
Q_OBJECT
public:
explicit Mafenetre(QWidget *parent = 0);
signals:
public slots:
};
#endif // MAFENETRE_H
Mafenetre.cpp:
#include "mafenetre.h"
Mafenetre::Mafenetre(QWidget *parent) :
QWidget(parent)
{
}
Finally you main.cpp should be like this, check the headers:
......
#include "mafenetre.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Mafenetre m;
m.show();
return a.exec();
}
I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.
For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.
Here is one simple way you could structure an application if you want an event loop running.
// main.cpp
#include <QtCore>
class Task : public QObject
{
Q_OBJECT
public:
Task(QObject *parent = 0) : QObject(parent) {}
public slots:
void run()
{
// Do processing here
emit finished();
}
signals:
void finished();
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Task parented to the application so that it
// will be deleted by the application.
Task *task = new Task(&a);
// This will cause the application to exit when
// the task signals finished.
QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
// This will run the task from the application event loop.
QTimer::singleShot(0, task, SLOT(run()));
return a.exec();
}
Don't forget to add the
CONFIG += console
flag in the qmake .pro file.
For the rest is just using some of Qt classes.
One way I use it is to spawn processes cross-platform.
You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:
#include <QtCore>
int main()
{
QVector<int> a; // Qt object
for (int i=0; i<10; i++)
{
a.append(i);
}
/* manipulate a here */
return 0;
}
I managed to create a simple console "hello world" with QT Creator
used creator 2.4.1 and QT 4.8.0 on windows 7
two ways to do this
Plain C++
do the following
File- new file project
under projects select : other Project
select "Plain C++ Project"
enter project name
5.Targets select Desktop 'tick it'
project managment just click next
you can use c++ commands as normal c++
or
QT Console
File- new file project
under projects select : other Project
select QT Console Application
Targets select Desktop 'tick it'
project managment just click next
add the following lines (all the C++ includes you need)
add "#include 'iostream' "
add "using namespace std; "
after QCoreApplication a(int argc, cghar *argv[])
10 add variables, and your program code..
example: for QT console "hello world"
file - new file project 'project name '
other projects - QT Console Application
Targets select 'Desktop'
project management - next
code:
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout<<" hello world";
return a.exec();
}
ctrl -R to run
compilers used for above MSVC 2010 (QT SDK) , and minGW(QT SDK)
hope this helps someone
As I have just started to use QT recently and also searched the Www for info and examples to get started with simple examples still searching...
You could fire an event into the quit() slot of your application even without connect().
This way, the event-loop does at least one turn and should process the events within your main()-logic:
#include <QCoreApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication app( argc, argv );
// do your thing, once
QTimer::singleShot( 0, &app, &QCoreApplication::quit );
return app.exec();
}
Don't forget to place CONFIG += console in your .pro-file, or set consoleApplication: true in your .qbs Project.CppApplication.
You can call QCoreApplication::exit(0) to exit with code 0
Had the same problem. found some videos on Youtube.
So here is an even simpler suggestion. This is all the code you need:
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug() <<"Hello World"<< endl;
return 0;
}
The above code comes from
Qt5 Tutorial: Building a simple Console application by
Dominique Thiebaut
http://www.youtube.com/watch?v=1_aF6o6t-J4