Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Console application with thread: It is not closed
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Console application with thread: It is not closed

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • francescmmF Offline
    francescmmF Offline
    francescmm
    wrote on last edited by
    #1

    I'm coding a console application that runs a QThread for connections and at same time, waits for user inputs by keyboard.

    My problem is that then of read what user's write, I send a signal to close the application and nothing happens. Is the first console appliation using Qt and I'm not sure if I'm following what Qt waits. Here is the code:

    @int main(int argc, char *argv[])
    {
    QCoreApplication app(argc, argv);
    CommandLineApp cmd;

    QObject::connect(&cmd, SIGNAL(quitApp()), &app, SLOT(quit()));
    
    cmd.exec();
    
    return app.exec();
    

    }@

    CommandLineApp.h (Server is a QTcpServer that has a QThread inside):
    @#ifndef CommandLineApp_H
    #define CommandLineApp_H

    #include <QObject>
    #include "Server.h"
    #include <iostream>

    using namespace std;

    class CommandLineApp : public QObject
    {
    Q_OBJECT

    signals:
        void quitApp();
    
    public:
        CommandLineApp(QObject *parent = 0);
        void exec&#40;&#41;;
    
    private:
        Server server;
    

    };

    #endif@

    CommandLineApp.cpp:
    @CommandLineApp::CommandLineApp(QObject *parent) : QObject(parent)
    {
    server.listen(QHostAddress::LocalHost,5555);
    }

    void CommandLineApp::exec()
    {
    string option;

    QTextStream cout(stdout&#41;;
    QTextStream cin(stdin);
    
    cout<<"Set option (help for Help, quit for Quit): ";
    cout.flush();
    cin>>option;
    
    server.close();
    
    emit quitApp();
    

    }
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You emit quitApp() before QCoreApplication enters it's event loop: that is, before signals and slots (and the whole Qt Meta Ojbect System) is able to work.

      (Z(:^

      1 Reply Last reply
      0
      • francescmmF Offline
        francescmmF Offline
        francescmm
        wrote on last edited by
        #3

        Arg! Thanks a lot!!

        Sometimes we are looking for complex problems and the problem is basic!

        Shoddy solution: add a timer with singleShot! :)

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          You are welcome.

          Solution good enough if it works :) Qt is more oriented towards GUI stuff (obviously), command line apps are sometimes a bit tricky.

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            Hi, IMO,

            @
            return app.exec();
            @

            can be simply replaced with

            @
            return 0;
            @

            As the event loop make non-sense in such case.

            1 Reply Last reply
            0
            • francescmmF Offline
              francescmmF Offline
              francescmm
              wrote on last edited by
              #6

              Thanks 1+1=2, it also works I've decided to to it to get more clear code.

              Mark the topic as solved!! :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved