Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
    QNetworkProxyFactory::setUseSystemConfiguration(true);
    Postmgr->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
    Postmgr2->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
    Getmgr->setProxy(QNetworkProxy(QNetworkProxy::NoProxy));
    
    
    for(int i=0;i<MAX_SCHEDULES;i++)
    {
        m_nProcess[i] = new QProcess(this);
    }
    
    m_FilesWatcher1 = new QFutureWatcher<void>;
    

    }

    HttpRequest::~HttpRequest()
    {

    }
    void HttpRequest::Start_Post_Thread(QString m_nIp,int m_nPort,QString m_nUser,QString m_nPassword)
    {
    if(m_FilesWatcher1->isRunning() == false)
    {
    m_FilesWatcher1->setFuture(QtConcurrent::run(this,&HttpRequest::Start_Http_Posting,m_nIp,m_nPort,m_nUser,m_nPassword));
    }
    }
    void HttpRequest::Start_Http_Posting(QString IPAdd2, int Port2, QString username2, QString Password2)
    {
    while(1)
    {
    Post_Mobile_Login(IPAdd2,Port2,username2,Password2);
    sleep(5);
    }
    }

    void HttpRequest::Post_Mobile_Login(QString IPAdd,int Port,QString username,QString Password)
    {
    QByteArray loginData;
    QString str = "user_name="+username+"&password="+Password;
    loginData.append(str);
    ServerUrl = "http://"+IPAdd+":"+QString::number(Port);
    QUrl request(ServerUrl+"/api/res_users/mobile_login?");
    QNetworkRequest r(request);
    r.setRawHeader("Content-Type", "application/json");
    r.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
    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

    Hi,
    To add to @Christian-Ehrlicher points:

    @satyanarayana143 said in Thread Issue:

    sleep(5);

    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.

    QTimer



  • @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",args.at(0));
        exit(1);
    }
    
    
    //m_nHttpRequest.Post_Mobile_Login(args.at(1),args.at(2).toInt(),args.at(3),args.at(4));
    m_nHttpRequest.Start_Post_Thread(args.at(1),args.at(2).toInt(),args.at(3),args.at(4));
    

    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