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. a main window class for console app - non GUI

a main window class for console app - non GUI

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.8k Views 2 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.
  • A Offline
    A Offline
    arsinte_andrei
    wrote on last edited by
    #1

    at the moment I have this

    int main(int argc, char *argv[]) {
    
    	QCoreApplication a(argc, argv);
    
    	myServer server;
    	server.listen(adr, port);
    
        return a.exec();
    }
    
    

    and I wish to move this bit

    atpTcpServer server;
    server.listen(adr, port);
    

    to a class - separate files myClass.h / myClass.cpp

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    
    #include <myServer.h>
    
    class myClass : public QObject {
    		Q_OBJECT
    
    	public:
    		explicit myClass(QObject *parent = nullptr);
    
    	signals:
    		void quit();
    
    	public slots:
    		void startAll();
    		void quitApp();
    
    	private slots:
    		void initIni();
    		bool initSQL();
    		void initServer();
    
    	private:
    		myServer server;
    
    };
    
    #endif 
    

    that is the h file and the c++ is the following

    #include "myClass.h"
    
    atpCommandsProcessing::atpCommandsProcessing(QObject *parent) : QObject(parent) {
    
    }
    
    void atpCommandsProcessing::startAll() {
    	initServer(); //start the server and some other stuff here
    
    }
    
    void atpCommandsProcessing::quitApp() {
    	emit quit();
    }
    
    void myClass::initServer() {
    	
    
    }
    
    

    and as soon as my main.cpp becomes like that

    int main(int argc, char *argv[]) {
    
        QCoreApplication a(argc, argv);
    
    	myClass myComPro;
    	myComPro.startAll();
    
        return a.exec();
    }
    
    

    after that my servers start but shortly after is destroyed.. why??? I think that my class is not in the event loop?? I wish to be like mainwindow class for the GUI sistem.. how can I do that?? to have a main window where to process everything and that not to be in main.cpp
    is that possible in Qt?? - hope it is...
    any advice??

    Many thanks

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

      Yes it is possible and your code looks correct, at least the part that you have shared.

      Some things to remember:

      • even loop starts in a.exec(). It is not running yet when you call myComPro.startAll()
      • if you want to start your code after even loop, use QTimer::singleShot(0, [&myComPro]() {myComPro.startAll()})
      • app won't quit until you stop the even loop (a.quit())

      (Z(:^

      J.HilkJ A 2 Replies Last reply
      1
      • sierdzioS sierdzio

        Yes it is possible and your code looks correct, at least the part that you have shared.

        Some things to remember:

        • even loop starts in a.exec(). It is not running yet when you call myComPro.startAll()
        • if you want to start your code after even loop, use QTimer::singleShot(0, [&myComPro]() {myComPro.startAll()})
        • app won't quit until you stop the even loop (a.quit())
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @sierdzio actually how does QCoreApplication behave, on default, when no Widget is opened before the event loop?

        I would assume, lastWindowClosed() is emitted. -> closing the application ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        sierdzioS 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @sierdzio actually how does QCoreApplication behave, on default, when no Widget is opened before the event loop?

          I would assume, lastWindowClosed() is emitted. -> closing the application ?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @J.Hilk said in a main window class for console app - non GUI:

          I would assume, lastWindowClosed() is emitted. -> closing the application ?

          Q Core Application ;-) No such signal there.

          (Z(:^

          J.HilkJ 1 Reply Last reply
          2
          • sierdzioS sierdzio

            @J.Hilk said in a main window class for console app - non GUI:

            I would assume, lastWindowClosed() is emitted. -> closing the application ?

            Q Core Application ;-) No such signal there.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @sierdzio
            x) my bad


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • sierdzioS sierdzio

              Yes it is possible and your code looks correct, at least the part that you have shared.

              Some things to remember:

              • even loop starts in a.exec(). It is not running yet when you call myComPro.startAll()
              • if you want to start your code after even loop, use QTimer::singleShot(0, [&myComPro]() {myComPro.startAll()})
              • app won't quit until you stop the even loop (a.quit())
              A Offline
              A Offline
              arsinte_andrei
              wrote on last edited by arsinte_andrei
              #6

              @sierdzio said in a main window class for console app - non GUI:

              Yes it is possible and your code looks correct, at least the part that you have shared.

              Some things to remember:

              • even loop starts in a.exec(). It is not running yet when you call myComPro.startAll()
              • if you want to start your code after even loop, use QTimer::singleShot(0, [&myComPro]() {myComPro.startAll()})
              • app won't quit until you stop the even loop (a.quit())

              I've just tried that and is giving me this

              QEventLoop: Cannot be used without QApplication
              atpTCPRunnable(0x555b6e1ba7c0) started on  QThread(0x555b6e1bae40, name = "Thread (pooled)")
              QEventLoop: Cannot be used without QApplication
              
              

              where should I use the Qapplication?? should I do the myComPro en extent of it??

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

                You have to put that timer after QCoreApplication a(argc, argv);.

                (Z(:^

                A 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  You have to put that timer after QCoreApplication a(argc, argv);.

                  A Offline
                  A Offline
                  arsinte_andrei
                  wrote on last edited by
                  #8

                  @sierdzio of course - this is what I've done but that was the error..

                  J.HilkJ 1 Reply Last reply
                  0
                  • A arsinte_andrei

                    @sierdzio of course - this is what I've done but that was the error..

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @arsinte_andrei can you please copy and paste your exact main.cpp ?


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    A 1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      arsinte_andrei
                      wrote on last edited by
                      #10

                      I've even tried
                      QMetaObject::invokeMethod( &comPro, "startAll", Qt::QueuedConnection );

                      1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @arsinte_andrei can you please copy and paste your exact main.cpp ?

                        A Offline
                        A Offline
                        arsinte_andrei
                        wrote on last edited by arsinte_andrei
                        #11

                        @J.Hilk yes here you have it a working one in the main.cpp

                        #include <QtCore>
                        #include <QCoreApplication>
                        #include <QtDebug>
                        #include <QFile>
                        #include <QTextStream>
                        #include <QtGlobal>
                        #include <stdio.h>
                        #include <stdlib.h>
                        #include <QDebug>
                        
                        #include <atpcommandsprocessing.h>
                        
                        
                        int main(int argc, char *argv[]) {
                        
                        	//TODO qInstallMessageHandler have to be enabled at the end
                        	//	qInstallMessageHandler(myMessageOutput); // Install the handler
                        
                        	QCoreApplication::setOrganizationName("@atp@");
                        	QCoreApplication::setOrganizationDomain("arsinte.co.uk");
                        	QCoreApplication::setApplicationName("atpServerSmartHomeSystem");
                        
                            QCoreApplication a(argc, argv);
                        
                        //	atpCommandsProcessing myComPro;
                        //	myComPro.startAll();
                        
                        
                        	atp_Ini atpInf;
                        
                        	QHostAddress adr = QHostAddress::Any;
                        	adr.setAddress(atpInf.get_ServerAddress());
                        	atpTcpServer server;
                        	server.setMode(static_cast<atpTcpServer::ThreadMode>( atpInf.get_ThreadMode()));
                        	server.setMaxConnections(atpInf.get_maxConnections());
                        	server.setConnectionTimeout(atpInf.get_timeOut());
                        	server.listen(adr, atpInf.get_ServerPortNo());
                        
                        //	QTimer::singleShot(0, [&myComPro]() {myComPro.startAll();});
                        
                        //	QObject::connect(&myComPro, &atpCommandsProcessing::quit, &a, &QCoreApplication::quit);
                        
                        
                            return a.exec();
                        }
                        
                        
                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          arsinte_andrei
                          wrote on last edited by
                          #12

                          Oh.. sorry ... I'm so stupid...
                          in my function * startAll* I had

                          atpTcpServer server;
                          

                          and as soon as the function finished even the server was closed... I've moved that declaration to the h file in private declarations and everything is working perfectly....

                          many Thanks to everyone - especially to @sierdzio

                          God bless

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

                            No problem, it happens to everybody sometimes :-) Happy coding!

                            (Z(:^

                            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