Solved 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 callmyComPro.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()
)
- even loop starts in
-
@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 ?
-
@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.
-
@sierdzio
x) my bad -
@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 callmyComPro.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??
- even loop starts in
-
You have to put that timer after
QCoreApplication a(argc, argv);
. -
@sierdzio of course - this is what I've done but that was the error..
-
@arsinte_andrei can you please copy and paste your exact main.cpp ?
-
I've even tried
QMetaObject::invokeMethod( &comPro, "startAll", Qt::QueuedConnection ); -
@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(); }
-
Oh.. sorry ... I'm so stupid...
in my function * startAll* I hadatpTcpServer 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
-
No problem, it happens to everybody sometimes :-) Happy coding!