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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4180394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:46:12  来源:igfitidea点击:

How do I create a simple Qt console application in C++?

c++qtconsole

提问by neuviemeporte

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:

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

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.

由于我不需要事件处理,我想知道如果我忽略创建 QCoreApplication 并运行事件循环是否会遇到麻烦。文档指出,在大多数情况下建议使用它。

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.

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

回答by baysmith

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

回答by fabrizioM

Don't forget to add the

不要忘记添加

CONFIG += console 

flag in the qmake .pro file.

qmake .pro 文件中的标志。

For the rest is just using some of Qt classes. One way I use it is to spawn processes cross-platform.

其余的只是使用一些 Qt 类。我使用它的一种方法是生成跨平台的进程。

回答by Simon Walker

You don't need the QCoreApplicationat all, just include your Qt objects as you would other objects, for example:

您根本不需要QCoreApplication,只需像其他对象一样包含您的 Qt 对象,例如:

#include <QtCore>

int main()
{
    QVector<int> a; // Qt object

    for (int i=0; i<10; i++)
    {
        a.append(i);
    }

    /* manipulate a here */

    return 0;
}

回答by code

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

我设法用 QT Creator 创建了一个简单的控制台“hello world”

used creator 2.4.1 and QT 4.8.0 on windows 7

在 Windows 7 上使用了 Creator 2.4.1 和 QT 4.8.0

two ways to do this

有两种方法可以做到这一点

Plain C++

普通 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++
  1. 文件-新建文件项目
  2. 在项目下选择:其他项目
  3. 选择“普通 C++ 项目”
  4. 输入项目名称 5.Targets 选择 Desktop 'tick it'
  5. 项目管理只需点击下一步
  6. 你可以像普通的 C++ 一样使用 C++ 命令

or

或者

QT Console

QT 控制台

  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..
  1. 文件-新建文件项目
  2. 在项目下选择:其他项目
  3. 选择 QT 控制台应用程序
  4. 目标选择桌面“勾选”
  5. 项目管理只需点击下一步
  6. 添加以下几行(您需要的所有 C++ 都包括在内)
  7. 添加“#include‘iostream’”
  8. 添加“使用命名空间标准;”
  9. 在 QCoreApplication a(int argc, cghar *argv[]) 10 之后添加变量,以及您的程序代码..

example: for QT console "hello world"

示例:对于 QT 控制台“hello world”

file - new file project 'project name '

文件 - 新文件项目“项目名称”

other projects - QT Console Application

其他项目 - QT 控制台应用程序

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

ctrl -R 运行

compilers used for above MSVC 2010 (QT SDK) , and minGW(QT SDK)

用于以上 MSVC 2010 (QT SDK) 和 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...

由于我最近刚刚开始使用 QT,并且还在 Www 上搜索了信息和示例,以开始使用仍在搜索的简单示例...

回答by Andrew

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

您可以调用 QCoreApplication::exit(0) 以代码 0 退出

回答by UnePierre

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:

即使没有connect(),您也可以在应用程序的quit() 槽中触发一个事件。这样,事件循环至少会转一圈,并且应该处理 main() 逻辑中的事件:

#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 += consolein your .pro-file, or set consoleApplication: truein your .qbs Project.CppApplication.

不要忘记放置CONFIG += console在您的 .pro 文件中,或设置consoleApplication: true在您的 .qbs Project.CppApplication 中。

回答by user3423766

Had the same problem. found some videos on Youtube. So here is an even simpler suggestion. This is all the code you need:

有同样的问题。在 Youtube 上找到了一些视频。所以这里有一个更简单的建议。这是您需要的所有代码:

#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

以上代码来自 Qt5 教程:Building a simple Console application by

Dominique Thiebaut

多米尼克·蒂博

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

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