Results 1 to 7 of 7

Thread: Console application beginning

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Console application beginning

    Hi all,

    newbie question after two years of honored Qt development
    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.

    Qt Code:
    1. // main.cpp
    2. #include <QtCore>
    3. #include "backdoor.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc,argv);
    8. Backdoor bd;
    9.  
    10. bd.spit("Hello world!");
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // backdoor.h
    2. #include <QtCore>
    3.  
    4. class Backdoor : public QObject
    5. {
    6. private:
    7.  
    8. public:
    9. Backdoor();
    10. void spit(QString str);
    11. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // backdoor.cpp
    2. #include "backdoor.h"
    3.  
    4. Backdoor::Backdoor()
    5. :out(stdout)
    6. {
    7. }
    8.  
    9. void Backdoor::spit(QString str)
    10. {
    11. out << str;
    12. }
    To copy to clipboard, switch view to plain text mode 
    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!
    Thanks in advance
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Console application beginning

    Use
    Qt Code:
    1. out << str << flush;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. out << str << endl;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to RSX for this useful post:

    Raccoon29 (17th October 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Console application beginning

    Quote Originally Posted by Raccoon29 View Post
    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...
    Last edited by wysota; 17th October 2009 at 11:56.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Console application beginning

    Are you compiling on Windows? By default on Windows there is no console output. Try add CONFIG += console to your project file.

  6. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Console application beginning

    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
    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)
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Console application beginning

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: Console application beginning

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

Similar Threads

  1. Replies: 8
    Last Post: 28th January 2015, 10:45
  2. about console application
    By jirach_gag in forum Qt Programming
    Replies: 2
    Last Post: 5th January 2012, 12:39
  3. Debugging a console application
    By Lorthirk in forum Qt Tools
    Replies: 1
    Last Post: 26th September 2009, 12:49
  4. Exiting a Qt Console Application
    By nbetcher in forum Qt Programming
    Replies: 4
    Last Post: 23rd March 2009, 22:03
  5. libGL using memory in console application
    By neuron in forum Qt Programming
    Replies: 0
    Last Post: 11th March 2009, 16:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.