Hello World (Console)

From qtnode

Jump to: navigation, search

Contents

Create the Source Files

Create a new file called hello.h containing:

#include <QObject>

class Hello : public QObject {
Q_OBJECT
signals:
    void done();
public slots:
    void world();
};

Create a new file called hello.cpp containing:

#include <QCoreApplication>
#include <QTextStream>
#include <QTimer>
#include "hello.h"

void Hello::world() {
    QTextStream qout(stdout);
    qout << "Hello, world!\n";
    emit done();
}

int main(int argc, char** argv) {
    QCoreApplication app(argc, argv);
    Hello hello;
    QObject::connect(&hello, SIGNAL(done()), &app, SLOT(quit()));
    QTimer::singleShot(1000, &hello, SLOT(world()));
    return app.exec();
}

Create a Project File

Create a new file called hello.pro containing:

TEMPLATE = app
TARGET = hello
HEADERS += hello.h
SOURCES += hello.cpp
CONFIG += qt console
QT -= gui

Compile the Project

  1. Execute the qmake command to create a suitable makefile.
  2. Execute the correct "make" command for your development environment. This could be make, mingw32-make, or nmake.

Run the Program

Under Windows, type hello. Under UNIX, type ./hello.

The program will start, wait one second, output the message "Hello, world!" and exit.

Personal tools