Important: Please read the Qt Code of Conduct -

Thread Issue

  • Hi Friends,

    HttpRequest::HttpRequest(QObject parent) : QObject(parent)
    Getmgr = new QNetworkAccessManager(this);
    QObject::connect(Getmgr, SIGNAL(finished(QNetworkReply
    )),this, SLOT(GetmanagerFinished(QNetworkReply*)));

    Postmgr = new QNetworkAccessManager(this);
    QObject::connect(Postmgr, SIGNAL(finished(QNetworkReply*)),this, SLOT(PostmanagerFinished(QNetworkReply*)));
    Postmgr2 = new QNetworkAccessManager(this);
    QObject::connect(Postmgr2, SIGNAL(finished(QNetworkReply*)),this, SLOT(PostmanagerFinished2(QNetworkReply*)));
    // no proxy
    for(int i=0;i<MAX_SCHEDULES;i++)
        m_nProcess[i] = new QProcess(this);
    m_FilesWatcher1 = new QFutureWatcher<void>;



    void HttpRequest::Start_Post_Thread(QString m_nIp,int m_nPort,QString m_nUser,QString m_nPassword)
    if(m_FilesWatcher1->isRunning() == false)
    void HttpRequest::Start_Http_Posting(QString IPAdd2, int Port2, QString username2, QString Password2)

    void HttpRequest::Post_Mobile_Login(QString IPAdd,int Port,QString username,QString Password)
    QByteArray loginData;
    QString str = "user_name="+username+"&password="+Password;
    ServerUrl = "http://"+IPAdd+":"+QString::number(Port);
    QUrl request(ServerUrl+"/api/res_users/mobile_login?");
    QNetworkRequest r(request);
    r.setRawHeader("Content-Type", "application/json");
    r.setRawHeader("Connection", "keep-alive");
    qDebug().noquote() << "############### loginData: ############"<< ServerUrl << loginData ;
    Postmgr->post(r, loginData);

    This is my code when i run this code i am getting error can anybody tell what is porblem and how to solve

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x562024c20f90), parent's thread is QThread(0x562024c13eb0), current thread is QThread(0x562024c326d0).

  • Lifetime Qt Champion

    There is absolutely no need to use a QThread for QNetworkAccessManager operations - they're all asynchron. So just stop using threads until you really need them.
    Also why do you create two instances of QNetworkAcessManager and what's the reason for the QProcess instances?

  • becuase i want post every 5sec once two instances for different post.

  • Lifetime Qt Champion

    This are still no reasons for two QNetworkAccessManager instances and even not for using QThread.

  • Lifetime Qt Champion

    To add to @Christian-Ehrlicher points:

    @satyanarayana143 said in Thread Issue:


    You're blocking the event loop here so your requests can't be properly handled.

    On a side note, QNetworkAccessManager handles up to 6 requests in parallel, after that their are queued for processing on a FIFO basis and as fast as possible. So again, don't involve threads until really necessary.

  • Post_Mobile_Login i want to call from mainc.pp every 5sec so i am using while loop with thread.

  • Lifetime Qt Champion

    @satyanarayana143 said in Thread Issue:

    i want to call from mainc.pp every 5sec

    Use a QTimer instead

  • Moderators

    @satyanarayana143 yeah, don't.

    You're not programming for a MicroController here, use the tools designed for such things.


  • @Christian-Ehrlicher

    in main.cpp

    this is code

    QCoreApplication a(argc, argv);
    HttpRequest m_nHttpRequest;
    QStringList args = a.arguments();

    if (args.count() != 5)
        qDebug("Usage: %s <IPAddress> <Port> <Username> <Password>  \n",;

    if i use timer to call Start_Post_Thread function how to pass arguments for timer signal.

  • Lifetime Qt Champion

    @satyanarayana143 said in Thread Issue:

    how to pass arguments for timer signal.

    There is no need to - you can e.g. pass the paramaeters to HttpRequest via ctor or setter functions - basic c++.

Log in to reply