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] SIGNALs and SLOTs in console mode
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] SIGNALs and SLOTs in console mode

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 8.3k 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.
  • V Offline
    V Offline
    vcsala
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vcsala
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

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

        }@

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vcsala
          wrote on last edited by
          #4

          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.

          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