Program Club

C ++로 간단한 Qt 콘솔 애플리케이션을 어떻게 생성합니까?

proclub 2020. 10. 29. 20:11
반응형

C ++로 간단한 Qt 콘솔 애플리케이션을 어떻게 생성합니까?


Qt의 XML 파서를 사용해보기 위해 간단한 콘솔 애플리케이션을 만들려고했습니다. VS2008에서 프로젝트를 시작했고 다음 템플릿을 얻었습니다.

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

    return a.exec();
}

이벤트 처리가 필요하지 않기 때문에 QCoreApplication을 생성하고 이벤트 루프를 실행하는 것을 게을리하면 문제가 발생할 수 있는지 궁금합니다. 문서에는 대부분의 경우 권장된다고 명시되어 있습니다.

그러나 호기심을 위해 이벤트 루프에서 일반 작업을 실행 한 다음 응용 프로그램을 종료하는 방법이 궁금합니다. 관련 예를 Google에 검색 할 수 없습니다.


다음은 이벤트 루프를 실행하려는 경우 응용 프로그램을 구성 할 수있는 간단한 방법입니다.

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

추가하는 것을 잊지 마십시오

CONFIG += console 

qmake .pro 파일의 플래그.

나머지는 Qt 클래스 중 일부를 사용하는 것입니다. 내가 사용하는 한 가지 방법은 크로스 플랫폼 프로세스를 생성하는 것입니다.


전혀 필요하지 않습니다. 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;
}

QT Creator로 간단한 콘솔 "hello world"를 만들 수있었습니다.

Windows 7에서 크리에이터 2.4.1 및 QT 4.8.0 사용

이를 수행하는 두 가지 방법

일반 C ++

다음을 수행

  1. 파일-새 파일 프로젝트
  2. 프로젝트에서 선택 : 기타 프로젝트
  3. "일반 C ++ 프로젝트"를 선택하십시오.
  4. 프로젝트 이름 입력 5. Targets 선택 Desktop 'tick it'
  5. 프로젝트 관리는 다음을 클릭하십시오.
  6. C ++ 명령을 일반 C ++로 사용할 수 있습니다.

또는

QT 콘솔

  1. 파일-새 파일 프로젝트
  2. 프로젝트에서 선택 : 기타 프로젝트
  3. QT 콘솔 애플리케이션 선택
  4. 대상은 데스크톱 '틱'을 선택합니다.
  5. 프로젝트 관리는 다음을 클릭하십시오.
  6. 다음 줄을 추가하십시오 (필요한 모든 C ++ 포함)
  7. 추가 "#include 'iostream'"
  8. "using namespace std;"추가
  9. QCoreApplication a (int argc, cghar * argv []) 10 후 변수와 프로그램 코드를 추가합니다.

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


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 processes 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.


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

참고URL : https://stackoverflow.com/questions/4180394/how-do-i-create-a-simple-qt-console-application-in-c

반응형