How to create a simple Qt console application in C ++?

I tried to create a simple console application to try out the Qt XML parser. I started the 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 handling, I was wondering if I could get into a problem if I neglected to create a QCoreApplication and start an event loop. Documents claim that it is recommended in most cases.

However, for the sake of curiosity, I wonder how I can perform some general task in the event loop and then shut down the application. I could not find a suitable Google example.

+50
c ++ qt console
Nov 14 '10 at 23:40
source share
6 answers

Here's an easy way to structure your application if you want a loop to work.

 // 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(); } 
+78
Nov 15 2018-10-11T00:
source share

Do not forget to add

 CONFIG += console 

in the qmake.pro file.

The rest is just using some Qt classes. One of the ways I use this is to start cross-platform processes.

+16
Nov 15 '10 at 4:15
source share

You do not need QCoreApplication at all, just include Qt objects, like 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; } 
+4
Nov 15 '10 at 0:02
source share

I managed to create a simple "hello world" console with QT Creator

Used Creator 2.4.1 and QT 4.8.0 for Windows 7

two ways to do it

Normal C ++

follow these steps

  • File-New Project
  • under select projects: other Project
  • select "Normal C ++ project"
  • enter project name 5.Targets select Desktop 'tick it'
  • Project management just click next
  • you can use C ++ commands as regular C ++

or

QT Console

  • File-New Project
  • under select projects: other Project
  • select console QT application
  • Goals select 'tick it' desktop
  • Project management just click next
  • add the following lines (all C ++ you need)
  • add "#include" iostream '"
  • add "using namespace std;"
  • after QCoreApplication a (int argc, cghar * argv []) 10 add the variables and your program code.

example: for QT console "hello world"

file - project name of the new project project

other projects - QT console application

Goals choose a desktop

project management - next

the 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

used for the above MSVC 2010 (QT SDK) and minGW (QT SDK)

hope this helps someone

Since I just recently started using QT, I also searched Www for information and examples to get started with simple examples that are still looking for ...

+4
Mar 23 2018-12-23T00:
source share

You can call QCoreApplication :: exit (0) to exit with code 0

+1
Nov 15 '10 at 4:15
source share

There was the same problem. found some videos on Youtube. So here is an even simpler sentence. This is the code you need:

 #include <QDebug> int main(int argc, char *argv[]) { qDebug() <<"Hello World"<< endl; return 0; } 

Above Code Qt5 Tutorial: Creating a Simple Console Application

Dominic Tibout

http://www.youtube.com/watch?v=1_aF6o6t-J4

-2
Mar 15 '14 at 17:32
source share



All Articles