PDA

View Full Version : Console application beginning



Raccoon29
17th October 2009, 02:12
Hi all,

newbie question after two years of honored Qt development :crying:
One sunny day I decided to kill myself trying to make up console applications...the beginning of the end.
I searched around for several hours, no examples, no a-b-c docs... I can't make a stupid hello world console application.



// main.cpp
#include <QtCore>
#include "backdoor.h"

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

bd.spit("Hello world!");

return a.exec();
}



// backdoor.h
#include <QtCore>

class Backdoor : public QObject
{
private:
QTextStream out;

public:
Backdoor();
void spit(QString str);
};



// backdoor.cpp
#include "backdoor.h"

Backdoor::Backdoor()
:out(stdout)
{
}

void Backdoor::spit(QString str)
{
out << str;
}

this code looks trivial, a stupid output, but I've not been able to make it run properly (no output!)...
1_ what's wrong with QTextStream? What did I forget?
2_ what is the basis concept to make console application? (an example of usage with an object and, possibly, an event to initiate me would be very very very apprecciated)

120 Experience Points to who'll answer good! :p
Thanks in advance

RSX
17th October 2009, 02:47
Use

out << str << flush;
or

out << str << endl;

wysota
17th October 2009, 11:22
1_ what's wrong with QTextStream? What did I forget?
You forgot to tell it where the output should go. Initiate it with stdout or stderr and it will work.

Edit: Aa... I just noticed you did :) Maybe you forgot to activate console support when running on Windows? Also remember to flush the stream at some point.


2_ what is the basis concept to make console application? (an example of usage with an object and, possibly, an event to initiate me would be very very very apprecciated

I don't really understand the question...

bender86
17th October 2009, 11:44
Are you compiling on Windows? By default on Windows there is no console output. Try add CONFIG += console to your project file.

Raccoon29
17th October 2009, 12:58
First really thank you to all RSX, Bender86 and S.Wysota!

@RSX & S.Wysota
out << str << flush;
worked! I wasn't aware that stream required flush... my bad.

Are you compiling on Windows? By default on Windows there is no console output. Try add CONFIG += console to your project file.
yes, I'm under Windows. I'm using QtCreator (THE amazing ultimate Qt IDE) and it provided that config for me when creating the new project.

@S.Wysota

Edit: Aa... I just noticed you did
it happens in the best families too :p

I don't really understand the question...
I bad explained. I thought that console application required some different structure than QApplication for example...but it was just a my wondering.

Another little question to close this thread (and complete the subject): what is "the most correct" way to close this application from Backdoor? I mean using QCoreApplication: some signal, some method...? (an example would be great)

wysota
17th October 2009, 13:02
QCoreApplication::quit()

code
24th March 2012, 00:42
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

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

QT Console

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