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. Thread Issue
Qt 6.11 is out! See what's new in the release blog

Thread Issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.1k Views 2 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.
  • S Offline
    S Offline
    satyanarayana143
    wrote on last edited by
    #1

    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).

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • S Offline
        S Offline
        satyanarayana143
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • S Offline
              S Offline
              satyanarayana143
              wrote on last edited by
              #6

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

              J.HilkJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                @satyanarayana143 said in Thread Issue:

                i want to call from mainc.pp every 5sec

                Use a QTimer instead

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                S 1 Reply Last reply
                2
                • S satyanarayana143

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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @satyanarayana143 yeah, don't.

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

                  QTimer


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • Christian EhrlicherC Christian Ehrlicher

                    @satyanarayana143 said in Thread Issue:

                    i want to call from mainc.pp every 5sec

                    Use a QTimer instead

                    S Offline
                    S Offline
                    satyanarayana143
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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++.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved