[SOLVED] SIGNALs and SLOTs in console mode



  • I am working on a small application which should run in console mode but uses QNetworkAccessManager. The main.cpp looks like this:

    @#include <QtCore/QCoreApplication>
    #include <QStringList>
    #include <QTimer>

    #include "clientlogin.h"

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

    if (QCoreApplication::arguments().count() < 2)
    {
        qDebug() << "Usage: enforceCaptcha <email address>";
        exit(1);
    
    }
    else
    {
        qDebug() << "Called with" << QCoreApplication::arguments().at(1);
        ClientLogin cl(0, QCoreApplication::arguments().at(1));
        QTimer::singleShot(0, &cl, SLOT(loginLoop()));
    }
    
    a.exec&#40;&#41;;
    

    }
    @

    and my pro file:

    @#-------------------------------------------------

    Project created by QtCreator 2010-12-16T20:11:33

    #-------------------------------------------------

    QT += core network
    QT -= gui

    TARGET = enforceCaptcha
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    clientlogin.cpp

    HEADERS +=
    clientlogin.h
    @

    ClientLogin class is inherited from QObject.

    The issue is that when I start the application (with an email address) it prints out the debug message then nothing happens. I know it is a noob question, but is the signal-slot mechanism works in console mode? Or what else can be the issue?



  • Some more additional info: in earlier version I left out a.exec() and called cl.loginLoop() directly. That way the first request was sent but no slot (nor the sslErrorHanlder nor the handleNetworkData) was called.



  • Hi,
    it could not work, as your object is located on the stack:

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

    if (QCoreApplication::arguments().count() < 2)
    {
        qDebug() << "Usage: enforceCaptcha <email address>";
        exit(1);
    
    }
    else
    {
        qDebug() << "Called with" << QCoreApplication::arguments().at(1);
        ClientLogin cl(0, QCoreApplication::arguments().at(1));
        QTimer::singleShot(0, &cl, SLOT(loginLoop()));
        // --&gt; here the cl object is destroyed.
    }
    
    a.exec&#40;&#41;;
    

    }@

    Move the event loop and the cl object in one scope, cl to outside or event loop to insite:

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

    if (QCoreApplication::arguments().count() < 2)
    {
        qDebug() << "Usage: enforceCaptcha <email address>";
        exit(1);
    
    }
    else
    {
        ClientLogin cl(0, QCoreApplication::arguments().at(1));
        qDebug() << "Called with" << QCoreApplication::arguments().at(1);
        QTimer::singleShot(0, &cl, SLOT(loginLoop()));
        a.exec&#40;&#41;;
    }
    

    }@



  • Thanks, this was the solution (and it was really a noob question :))

    I beleive as I reedited the code several times to solve the issue I just forgot to check if it is still correct from C++ point of view.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.