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] child object working in thread
QtWS25 Last Chance

[solved] child object working in thread

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 3.4k Views
  • 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.
  • J Offline
    J Offline
    jigar.patel
    wrote on last edited by
    #1

    @
    //=============================================================================
    class myobject: public QObject
    {
    Q_OBJECT
    public:
    QTimer *tt;
    explicit myobject(QObject *parent = 0);
    public slots:
    //void myprocess();
    void ttevent();
    };
    //=============================================================================
    class mainobject: public QObject
    {
    Q_OBJECT
    public:
    explicit mainobject(QObject *parent = 0);
    myobject *ob1;
    public slots:
    //void myprocess();
    void mainprocess();
    };
    //=============================================================================
    myobject::myobject(QObject *parent) :
    QObject(parent)
    {
    qDebug() << "Constructor myobject, THREAD :" << QThread::currentThreadId();
    tt = new QTimer(this);
    connect (tt, SIGNAL(timeout()), this, SLOT(ttevent()));
    tt->start(5000);
    qDebug() << "Parent of tt is" << tt->parent();
    }
    //=============================================================================
    mainobject::mainobject(QObject *parent) :
    QObject(parent)
    {
    qDebug() << "Constructor mainobject, THREAD :" << QThread::currentThreadId();
    ob1 = new myobject(this);
    }
    //=============================================================================
    void mainobject::mainprocess()
    {
    while(1)
    {
    qDebug()<<"jai shree ram";
    sleep(2);
    }
    }
    //=============================================================================
    void myobject::ttevent()
    {
    qDebug()<<"jai shree krishna";
    }
    //=============================================================================
    //in main function
    QThread *Op1 = new QThread;
    mainobject *f1 = new mainobject(0);
    f1->moveToThread(Op1);
    QObject::connect(Op1,SIGNAL(started()),f1,SLOT(mainprocess()));
    Op1->start();
    //=============================================================================
    @
    by above code, only jai shree ram is printing every 2 second. jai shree krishna is not printing every 5 second.why?

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

      Hi,

      Because mainprocess is an infinite loop that doesn't let Qt's own event loop run so no signal can be emitted thus no slot are called

      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
      0
      • J Offline
        J Offline
        jigar.patel
        wrote on last edited by
        #3

        @
        //====================================================
        void mythread::run()
        {
        QTimer *jj = new QTimer(0);
        jj->setInterval(1000);
        connect(jj,SIGNAL(timeout()),this,SLOT(runevent()));
        jj->start();
        exec();
        while(1);

        }
        //====================================================
        void mythread::runevent()
        {
        qDebug()<<"yahoo";
        }
        //====================================================
        class mythread:public QThread
        {
        Q_OBJECT

        protected:
        void run();
        public slots:
        void runevent();
        };
        //====================================================
        @

        when i create one thread of mythread in main, "yahoo" will be printed every 1 second inspite of "while(1)"(infinite loop) in run.
        i have used event loop exec() for that. but if i want to do same by previous method(movetothread) then what to do?

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

          In your run method, you don't reach the while statement, exec is blocking, so you have an event loop running and only when ending it (e.g. calling QThread's quit) you'll enter the while loop.

          Just create a myobject and move it to a QThread, no need for any while loop in this case

          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
          0
          • J Offline
            J Offline
            jigar.patel
            wrote on last edited by
            #5

            myobject is already child of mainobject (see first code). so when i move main object, then my object is autometically move to thread. that i have checked. but timer which is initialized in constructor of my object will never timeout & tt event will never fired.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jigar.patel
              wrote on last edited by
              #6

              can you show me how to write infinite loop code in thread. qtimer & qnetworkaccessmanager will also be used in thread. i also need events of qtimer & qnetwork accessmanager.

              can it possible?

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

                The child objects of your moved objects will also get moved, by child object I mean a QObject derivative that has it's parent set the object to be moved.

                Putting an infinite loop in a thread that needs qtimer and qnam sounds like a bad design idea. Have a look at the threaded fortune server and client example in Qt's documentation to get an idea on how to start

                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
                0
                • J Offline
                  J Offline
                  jigar.patel
                  wrote on last edited by
                  #8

                  then how to design firmware to achieve my goal. should i use signal & slots? or should i use multiple threads?

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

                    I can't answer that question, you did not tell what is your exact goal is

                    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
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      [quote author="jigar.patel" date="1379751702"]should i use signal & slots? or should i use multiple threads?[/quote]Signals and slots work can work with multiple threads IF you don't use infinite loops (while(1)). If your thread has an infinite loop, it cannot use QTimer and QNetworkAccessManager, because they require signals and slots.

                      Read http://qt-project.org/doc/qt-5.1/qtcore/qthread.html for examples

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jigar.patel
                        wrote on last edited by
                        #11

                        all of you, thank you very much, for guiding me.
                        It is very helpfull to me.
                        now i have successfully used timer & networking in thread.

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

                          You're welcome !

                          Since you have everything working, don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)

                          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
                          0
                          • J Offline
                            J Offline
                            jigar.patel
                            wrote on last edited by
                            #13

                            Finally i have removed infinite loop from my thread main function & i have put it in timer event. so now i can successfully use network management & other timers.

                            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