如何在 c + + 中创建一个简单的 Qt 控制台应用?

我试图创建一个简单的控制台应用来尝试 Qt 的 XML 解析器。我在 VS2008中开始了一个项目,得到了这个模板:

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);


return a.exec();
}

因为我不需要事件处理,所以我想知道如果我忽略创建 QCoreApplication 并运行事件循环是否会遇到麻烦。医生说大多数情况下都建议这么做。

然而,出于好奇,我想知道如何在事件循环中执行一些通用任务,然后终止应用程序。我无法谷歌一个相关的例子。

128017 次浏览

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;
}

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 can call QCoreApplication::exit(0) to exit with code 0

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();
}

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

  1. File- new file project
  2. under projects select : other Project
  3. select "Plain C++ Project"
  4. enter project name 5.Targets select Desktop 'tick it'
  5. project managment just click next
  6. you can use c++ commands as normal c++

or

QT Console

  1. File- new file project
  2. under projects select : other Project
  3. select QT Console Application
  4. Targets select Desktop 'tick it'
  5. project managment just click next
  6. add the following lines (all the C++ includes you need)
  7. add "#include 'iostream' "
  8. add "using namespace std; "
  9. 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...

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

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.