How to Create a Simple Qt Console Application in C++

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

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

Can I use Qt Creator to create a console application?

Sure, you can. Here below I show you the screenshot of QtCreator's "New project" form:

QtCreator "New project" form

As you can see you can easily select to create a standard "C++" project (making use of qmake) or also a standard "C++" project (making use of cmake).

Your plain C++ project can be a simple console application (making use of cin and cout for example).

Creating QT Application as GUI for existing console-based application on windows

I found a solution for my needs and can do what i want to do.. Actually i'm a bit disappointed. I thought it would be something more complex.

First i have to say it's an QtQuick Application .. Maybe i should have said that earlier.

I simply outsourced the process functions to another class. This class is passed to QML via the qmlRegisterType<>() function. I connected some signals from the process ( QProcess ) to slots in my own class and wrote my own functions to handle reading/writing data from and to the console application. With the QML-onClicked events i can pass my parameters and strings to the console app. And with some application logic i can handle the in/out requests and timings.

WrapperClass.h

class WrapperClass: public QObject
{
Q_OBJECT

public:
explicit WrapperClass(QObject *parent = nullptr);

QProcess *process;
QString str_proc_output;

Q_INVOKABLE void startProcess();
Q_INVOKABLE void stopProcess();

Q_INVOKABLE QString getOutput();
Q_INVOKABLE void writeByte(QString str);

Q_INVOKABLE QString getAllOutput();
private:

signals:

public slots:
void mReadyRead();
void mReadyReadStandardOutput();
void mFinished(int code);
void mBytesWritten(qint64 written);

};

WrapperClass.cpp

WrapperClass::WrapperClass(QObject *parent) : QObject(parent)
{
process = new QProcess();
process->setProgram("untitled.exe");
process->setProcessChannelMode(QProcess::MergedChannels);

str_proc_output = "";

connect(process, SIGNAL(readyRead()), this, SLOT(mReadyRead()));
connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(mReadyReadStandardOutput()));
connect(process, SIGNAL(finished(int)), this, SLOT(mFinished(int)));
connect(process, SIGNAL(bytesWritten(qint64)), this, SLOT(mBytesWritten(qint64)));

}

void WrapperClass::startProcess() {
if(process->state() == QProcess::Running) {
stopProcess();
} else {
process->open(QProcess::ReadWrite);
}
}

void WrapperClass::stopProcess() {
process->close();
}

QString WrapperClass::getOutput() {
return str_proc_output;
}

QString WrapperClass::getAllOutput() {
QString str = process->readAll();

std::cout << str.toStdString() << std::endl;
return str;
}

void WrapperClass::writeByte(QString str) {

char cArr[str.length()] = {};

memcpy(cArr, str.toStdString().c_str(), str.length());

QByteArray arr = QByteArray(cArr, -1);
process->write(arr);
}

void WrapperClass::mReadyRead() {
QString s = QString(process->readAll());

std::cout << "ReadyRead: " << s.toStdString() << std::endl;
str_proc_output = s;
}

void WrapperClass::mReadyReadStandardOutput() {
QString s = QString(process->readAllStandardOutput());

std::cout << "ReadyReadStandardOutput: " << s.toStdString() << std::endl;

}

void WrapperClass::mFinished(int code) {
std::cout << "Process finished! (" << code << ')' << std::endl;
}

void WrapperClass::mBytesWritten(qint64 written) {

std::cout << "Bytes written: " << written << std::endl;

}

Main.cpp

int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);

qmlRegisterType<WrapperClass>("com.example.WrapperClass", 0, 1, "WrapperClass");

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

return app.exec();
}

With registering the Cpp-Class to QML i'm able to trigger the read/write functions via Click-Events from QML-MouseArea or Button.

Display a Console in Qt

By default, GUI apps don't have the terminal enabled in QtCreator. Enabling it is simple:

  • In the left sidebar, click Projects
  • At the top, select your project's Run tab
  • In the Run section, check the Run in terminal checkbox

Sample Image

How can i create a Single exe of QT Console Application , with static libraries,

libgcc_s_dw2-1.dll mingwm10.dll

Those libraries are dependencies of programs built by MinGW compiler. To get rid of those dll's and if I remember correctly use LIBS += -static. If you want to link Qt libraries statically, than you should build qt libraries statically

UPDATE


If you want to get rid of those 2 dll's I mentioned above, put LIBS += -static in your .pro file.

If you want to get rid of dlls such as QtCore4.dll QtGui4.dll etc, you should rebuild Qt source code statically. If you go this way, you should first choose which compiler you want to use. Currently I'm using MSVC 2010. Just download latest qt sources, execute configure.exe with the following parameters: -debug-and-release -platform win32-msvc2010 -sse -sse2 -no-qt3support -no-s60 -no-cetest -saveconfig config -mp and follow instructions (Keep in mind that you should have MSVC compiler installed. Just download MSVC 2010 express, it's free). Compilation will take several hours. When it's done, you should register your newly compiled Qt libraries in QtCreator. To do this, launch QtCreator -> Tools -> Options -> Build & run -> Qt Versions -> Add. When new dialog pops up, navigate to the folder where your qt source code resides, go to bin and select qmake.exe. Enter the name of qt version, for example: "Qt Static" and that's it. After that you will be able to choose your new qt libraries from project settings.

Hope it helps, if there's something not clear for you, feel free to ask.



Related Topics



Leave a reply



Submit