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












63















I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:



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

return a.exec();
}


Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.



For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.










share|improve this question

























  • Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

    – John Dibling
    Nov 14 '10 at 23:51











  • You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

    – Jeremy Friesner
    Nov 14 '10 at 23:58













  • what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

    – ianmac45
    Nov 15 '10 at 1:14






  • 1





    Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

    – neuviemeporte
    Nov 15 '10 at 10:13
















63















I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:



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

return a.exec();
}


Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.



For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.










share|improve this question

























  • Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

    – John Dibling
    Nov 14 '10 at 23:51











  • You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

    – Jeremy Friesner
    Nov 14 '10 at 23:58













  • what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

    – ianmac45
    Nov 15 '10 at 1:14






  • 1





    Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

    – neuviemeporte
    Nov 15 '10 at 10:13














63












63








63


20






I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:



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

return a.exec();
}


Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.



For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.










share|improve this question
















I was trying to create a simple console application to try out Qt's XML parser. I started a project in VS2008 and got this template:



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

return a.exec();
}


Since I don't need event processing, I was wondering whether I may get into trouble if I neglect to create a QCoreApplication and running the event loop. The docs state that it's recommended in most cases.



For the sake of curiosity however, I am wondering how could I make some generic task execute on the event loop and then terminate the application. I was unable to google a relevant example.







c++ qt console






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '10 at 10:19







neuviemeporte

















asked Nov 14 '10 at 23:40









neuviemeporteneuviemeporte

3,16293868




3,16293868













  • Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

    – John Dibling
    Nov 14 '10 at 23:51











  • You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

    – Jeremy Friesner
    Nov 14 '10 at 23:58













  • what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

    – ianmac45
    Nov 15 '10 at 1:14






  • 1





    Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

    – neuviemeporte
    Nov 15 '10 at 10:13



















  • Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

    – John Dibling
    Nov 14 '10 at 23:51











  • You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

    – Jeremy Friesner
    Nov 14 '10 at 23:58













  • what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

    – ianmac45
    Nov 15 '10 at 1:14






  • 1





    Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

    – neuviemeporte
    Nov 15 '10 at 10:13

















Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

– John Dibling
Nov 14 '10 at 23:51





Question is too vague. If you are having a specific problem, post the code and post any errors. If you are look for a how-to, google is your best friend.

– John Dibling
Nov 14 '10 at 23:51













You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

– Jeremy Friesner
Nov 14 '10 at 23:58







You only need to call exec() if you want to run an event loop; it sounds like you don't want to do that. That said, if you do end up wanting to run an event loop, you can cause the event loop to exit by calling qApp->quit() (where qApp is a global variable that Qt sets to point to your QApplication object)

– Jeremy Friesner
Nov 14 '10 at 23:58















what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

– ianmac45
Nov 15 '10 at 1:14





what jeremy said: DO NOT CALL THE EXEC(). you don't need the event loop.

– ianmac45
Nov 15 '10 at 1:14




1




1





Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

– neuviemeporte
Nov 15 '10 at 10:13





Sorry if this is too vague, but I had no idea how to expand it. The problem was that I was led to believe by the docs, that this template, while not generally imperative, is actually The Right Way to do things. One question then was - is this true, or can I skip it, and it was satisfactorily answered. The other question was, in essence: how do I make stuff run on the event loop and then exit. Editing to reflect, perhaps this'll make some difference.

– neuviemeporte
Nov 15 '10 at 10:13












7 Answers
7






active

oldest

votes


















91














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





share|improve this answer



















  • 2





    @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

    – Erik Sjölund
    Jun 1 '13 at 9:16











  • QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

    – Answeror
    Jun 21 '13 at 2:03






  • 3





    why do you include main.moc?

    – Yola
    Jul 28 '17 at 10:57



















17














Don't forget to add the



CONFIG += console 


flag in the qmake .pro file.



For the rest is just using some of Qt classes.
One way I use it is to spawn processes cross-platform.






share|improve this answer































    4














    You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:



    #include <QtCore>

    int main()
    {
    QVector<int> a; // Qt object

    for (int i=0; i<10; i++)
    {
    a.append(i);
    }

    /* manipulate a here */

    return 0;
    }





    share|improve this answer



















    • 4





      From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

      – neuviemeporte
      Nov 15 '10 at 0:13






    • 2





      What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

      – wrosecrans
      Nov 15 '10 at 6:50











    • To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

      – neuviemeporte
      Nov 15 '10 at 10:05



















    3














    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




    1. File- new file project

    2. under projects select : other Project

    3. select "Plain C++ Project"

    4. enter project name
      5.Targets select Desktop 'tick it'

    5. project managment just click next

    6. you can use c++ commands as normal c++


    or



    QT Console




    1. File- new file project

    2. under projects select : other Project

    3. select QT Console Application

    4. Targets select Desktop 'tick it'

    5. project managment just click next

    6. add the following lines (all the C++ includes you need)

    7. add "#include 'iostream' "

    8. add "using namespace std; "

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






    share|improve this answer































      1














      You can call QCoreApplication::exit(0) to exit with code 0






      share|improve this answer































        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.






        share|improve this answer































          -1














          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






          share|improve this answer





















          • 2





            This does not use the Qt event loop. It is a bare Unix main().

            – Raúl Salinas-Monteagudo
            Mar 7 '16 at 21:58











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4180394%2fhow-do-i-create-a-simple-qt-console-application-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          91














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





          share|improve this answer



















          • 2





            @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

            – Erik Sjölund
            Jun 1 '13 at 9:16











          • QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

            – Answeror
            Jun 21 '13 at 2:03






          • 3





            why do you include main.moc?

            – Yola
            Jul 28 '17 at 10:57
















          91














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





          share|improve this answer



















          • 2





            @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

            – Erik Sjölund
            Jun 1 '13 at 9:16











          • QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

            – Answeror
            Jun 21 '13 at 2:03






          • 3





            why do you include main.moc?

            – Yola
            Jul 28 '17 at 10:57














          91












          91








          91







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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '10 at 7:06









          baysmithbaysmith

          4,4521817




          4,4521817








          • 2





            @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

            – Erik Sjölund
            Jun 1 '13 at 9:16











          • QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

            – Answeror
            Jun 21 '13 at 2:03






          • 3





            why do you include main.moc?

            – Yola
            Jul 28 '17 at 10:57














          • 2





            @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

            – Erik Sjölund
            Jun 1 '13 at 9:16











          • QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

            – Answeror
            Jun 21 '13 at 2:03






          • 3





            why do you include main.moc?

            – Yola
            Jul 28 '17 at 10:57








          2




          2





          @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

          – Erik Sjölund
          Jun 1 '13 at 9:16





          @baysmith In qt5 this QObject::connect(task, &Task::finished, &QCoreApplication::quit); seems to work too.

          – Erik Sjölund
          Jun 1 '13 at 9:16













          QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

          – Answeror
          Jun 21 '13 at 2:03





          QCoreApplication::quit is a slot: QTimer::singleShot(0, &a, SLOT(quit()));

          – Answeror
          Jun 21 '13 at 2:03




          3




          3





          why do you include main.moc?

          – Yola
          Jul 28 '17 at 10:57





          why do you include main.moc?

          – Yola
          Jul 28 '17 at 10:57













          17














          Don't forget to add the



          CONFIG += console 


          flag in the qmake .pro file.



          For the rest is just using some of Qt classes.
          One way I use it is to spawn processes cross-platform.






          share|improve this answer




























            17














            Don't forget to add the



            CONFIG += console 


            flag in the qmake .pro file.



            For the rest is just using some of Qt classes.
            One way I use it is to spawn processes cross-platform.






            share|improve this answer


























              17












              17








              17







              Don't forget to add the



              CONFIG += console 


              flag in the qmake .pro file.



              For the rest is just using some of Qt classes.
              One way I use it is to spawn processes cross-platform.






              share|improve this answer













              Don't forget to add the



              CONFIG += console 


              flag in the qmake .pro file.



              For the rest is just using some of Qt classes.
              One way I use it is to spawn processes cross-platform.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '10 at 4:15









              fabrizioMfabrizioM

              30k126494




              30k126494























                  4














                  You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:



                  #include <QtCore>

                  int main()
                  {
                  QVector<int> a; // Qt object

                  for (int i=0; i<10; i++)
                  {
                  a.append(i);
                  }

                  /* manipulate a here */

                  return 0;
                  }





                  share|improve this answer



















                  • 4





                    From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                    – neuviemeporte
                    Nov 15 '10 at 0:13






                  • 2





                    What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                    – wrosecrans
                    Nov 15 '10 at 6:50











                  • To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                    – neuviemeporte
                    Nov 15 '10 at 10:05
















                  4














                  You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:



                  #include <QtCore>

                  int main()
                  {
                  QVector<int> a; // Qt object

                  for (int i=0; i<10; i++)
                  {
                  a.append(i);
                  }

                  /* manipulate a here */

                  return 0;
                  }





                  share|improve this answer



















                  • 4





                    From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                    – neuviemeporte
                    Nov 15 '10 at 0:13






                  • 2





                    What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                    – wrosecrans
                    Nov 15 '10 at 6:50











                  • To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                    – neuviemeporte
                    Nov 15 '10 at 10:05














                  4












                  4








                  4







                  You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:



                  #include <QtCore>

                  int main()
                  {
                  QVector<int> a; // Qt object

                  for (int i=0; i<10; i++)
                  {
                  a.append(i);
                  }

                  /* manipulate a here */

                  return 0;
                  }





                  share|improve this answer













                  You don't need the QCoreApplication at all, just include your Qt objects as you would other objects, for example:



                  #include <QtCore>

                  int main()
                  {
                  QVector<int> a; // Qt object

                  for (int i=0; i<10; i++)
                  {
                  a.append(i);
                  }

                  /* manipulate a here */

                  return 0;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '10 at 0:02









                  Simon WalkerSimon Walker

                  1,90332228




                  1,90332228








                  • 4





                    From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                    – neuviemeporte
                    Nov 15 '10 at 0:13






                  • 2





                    What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                    – wrosecrans
                    Nov 15 '10 at 6:50











                  • To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                    – neuviemeporte
                    Nov 15 '10 at 10:05














                  • 4





                    From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                    – neuviemeporte
                    Nov 15 '10 at 0:13






                  • 2





                    What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                    – wrosecrans
                    Nov 15 '10 at 6:50











                  • To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                    – neuviemeporte
                    Nov 15 '10 at 10:05








                  4




                  4





                  From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                  – neuviemeporte
                  Nov 15 '10 at 0:13





                  From the docs: "Some Qt classes, such as QString, can be used without a QCoreApplication object. However, in general, we recommend that you create a QCoreApplication or a QApplication object in your main() function as early as possible.". What if I want to be careful, or perhaps plan to utilize the event loop in the future? I assume I should create some Q_OBJECT type class and do my work inside it - what should it look like?

                  – neuviemeporte
                  Nov 15 '10 at 0:13




                  2




                  2





                  What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                  – wrosecrans
                  Nov 15 '10 at 6:50





                  What it should look like is going to be influenced by what you want it to do. If you eventually want to do something that uses the event loop, then use the event loop. Since you haven't nailed down the problem you are trying to solve, any answers will be maddeningly zenlike in their generality. It's like asking us what your book would look like, if you were to write a book. But, you don't tell us what sort of book you want to write.

                  – wrosecrans
                  Nov 15 '10 at 6:50













                  To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                  – neuviemeporte
                  Nov 15 '10 at 10:05





                  To carry that analogy, in the most general case, a book has a cover and pages in between. I think baysmith succeeded in showing the general view of such covers, that is how to run something on the event loop end return thence.

                  – neuviemeporte
                  Nov 15 '10 at 10:05











                  3














                  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




                  1. File- new file project

                  2. under projects select : other Project

                  3. select "Plain C++ Project"

                  4. enter project name
                    5.Targets select Desktop 'tick it'

                  5. project managment just click next

                  6. you can use c++ commands as normal c++


                  or



                  QT Console




                  1. File- new file project

                  2. under projects select : other Project

                  3. select QT Console Application

                  4. Targets select Desktop 'tick it'

                  5. project managment just click next

                  6. add the following lines (all the C++ includes you need)

                  7. add "#include 'iostream' "

                  8. add "using namespace std; "

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






                  share|improve this answer




























                    3














                    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




                    1. File- new file project

                    2. under projects select : other Project

                    3. select "Plain C++ Project"

                    4. enter project name
                      5.Targets select Desktop 'tick it'

                    5. project managment just click next

                    6. you can use c++ commands as normal c++


                    or



                    QT Console




                    1. File- new file project

                    2. under projects select : other Project

                    3. select QT Console Application

                    4. Targets select Desktop 'tick it'

                    5. project managment just click next

                    6. add the following lines (all the C++ includes you need)

                    7. add "#include 'iostream' "

                    8. add "using namespace std; "

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






                    share|improve this answer


























                      3












                      3








                      3







                      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




                      1. File- new file project

                      2. under projects select : other Project

                      3. select "Plain C++ Project"

                      4. enter project name
                        5.Targets select Desktop 'tick it'

                      5. project managment just click next

                      6. you can use c++ commands as normal c++


                      or



                      QT Console




                      1. File- new file project

                      2. under projects select : other Project

                      3. select QT Console Application

                      4. Targets select Desktop 'tick it'

                      5. project managment just click next

                      6. add the following lines (all the C++ includes you need)

                      7. add "#include 'iostream' "

                      8. add "using namespace std; "

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






                      share|improve this answer













                      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




                      1. File- new file project

                      2. under projects select : other Project

                      3. select "Plain C++ Project"

                      4. enter project name
                        5.Targets select Desktop 'tick it'

                      5. project managment just click next

                      6. you can use c++ commands as normal c++


                      or



                      QT Console




                      1. File- new file project

                      2. under projects select : other Project

                      3. select QT Console Application

                      4. Targets select Desktop 'tick it'

                      5. project managment just click next

                      6. add the following lines (all the C++ includes you need)

                      7. add "#include 'iostream' "

                      8. add "using namespace std; "

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







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 23 '12 at 23:07









                      codecode

                      551




                      551























                          1














                          You can call QCoreApplication::exit(0) to exit with code 0






                          share|improve this answer




























                            1














                            You can call QCoreApplication::exit(0) to exit with code 0






                            share|improve this answer


























                              1












                              1








                              1







                              You can call QCoreApplication::exit(0) to exit with code 0






                              share|improve this answer













                              You can call QCoreApplication::exit(0) to exit with code 0







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '10 at 4:15









                              AndrewAndrew

                              19.4k85183




                              19.4k85183























                                  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.






                                  share|improve this answer




























                                    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.






                                    share|improve this answer


























                                      0












                                      0








                                      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.






                                      share|improve this answer













                                      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.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 19 '18 at 9:03









                                      UnePierreUnePierre

                                      113




                                      113























                                          -1














                                          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






                                          share|improve this answer





















                                          • 2





                                            This does not use the Qt event loop. It is a bare Unix main().

                                            – Raúl Salinas-Monteagudo
                                            Mar 7 '16 at 21:58
















                                          -1














                                          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






                                          share|improve this answer





















                                          • 2





                                            This does not use the Qt event loop. It is a bare Unix main().

                                            – Raúl Salinas-Monteagudo
                                            Mar 7 '16 at 21:58














                                          -1












                                          -1








                                          -1







                                          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






                                          share|improve this answer















                                          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







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited May 16 '14 at 16:00









                                          Validus Oculus

                                          1,1191925




                                          1,1191925










                                          answered Mar 15 '14 at 17:32









                                          user3423766user3423766

                                          231




                                          231








                                          • 2





                                            This does not use the Qt event loop. It is a bare Unix main().

                                            – Raúl Salinas-Monteagudo
                                            Mar 7 '16 at 21:58














                                          • 2





                                            This does not use the Qt event loop. It is a bare Unix main().

                                            – Raúl Salinas-Monteagudo
                                            Mar 7 '16 at 21:58








                                          2




                                          2





                                          This does not use the Qt event loop. It is a bare Unix main().

                                          – Raúl Salinas-Monteagudo
                                          Mar 7 '16 at 21:58





                                          This does not use the Qt event loop. It is a bare Unix main().

                                          – Raúl Salinas-Monteagudo
                                          Mar 7 '16 at 21:58


















                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4180394%2fhow-do-i-create-a-simple-qt-console-application-in-c%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          How to send String Array data to Server using php in android

                                          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                          Is anime1.com a legal site for watching anime?