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. Wait until slot end to continue program execution

Wait until slot end to continue program execution

Scheduled Pinned Locked Moved Solved General and Desktop
qcanbusdevice
10 Posts 3 Posters 3.6k 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.
  • B Offline
    B Offline
    Babs
    wrote on 17 Apr 2019, 08:31 last edited by
    #1

    Hello, I'm working on a CanBus API. I 'm storing received data from my device in a QStringList. There i connect the signal QCanBusBusFrame::Received frame to a slot processFrame.
    The problem is when i read the QStringList in my main function it is empty. Seconds after it is filled.
    Here is my connection and code:

    void DevicesManagement::processReceivedFrames()
    {
        if(!m_device)
            return;
        while (m_device->framesAvailable()) {
            const QCanBusFrame frame = m_device->readFrame();
            m_receivedFrames.append(frame.payload().toHex());
            set_deviceProfile(frame.payload().toHex().right(8).toUpper());
        }
        emit processOver();
        qDebug()<<get_deviceProfile();
        disconnect();
    }
    

    And i connected my slot to the signal like this:

        connect(m_device, &QCanBusDevice::framesReceived, this, &DevicesManagement::processReceivedFrames);
    
    

    And there is my main

    int main (int argc, char * argv []) {
        QGuiApplication app (argc, argv);
        QQmlApplicationEngine engine (&app);
        registerQtQmlTricksUiElements (&engine);
        registerQtQmlTricksSmartDataModel (&engine);
        DevicesManagement *manager=new DevicesManagement(&engine);
        QList<QObject*>mDeviceList;
        manager->initializeDevices("125000");
        manager->deviceProfileProcess();
        qDebug()<<manager->get_deviceProfile();
     
    
        engine.load (QUrl ("qrc:///splash_power.qml"));
    
        return app.exec ();
    }
    
    

    The problem is that my slot processReceivedFrames is not over yet when i read my frame. Therefore it is empty.
    Anyone got an idea to solve that issue?

    J 1 Reply Last reply 17 Apr 2019, 08:36
    0
    • B Babs
      17 Apr 2019, 08:31

      Hello, I'm working on a CanBus API. I 'm storing received data from my device in a QStringList. There i connect the signal QCanBusBusFrame::Received frame to a slot processFrame.
      The problem is when i read the QStringList in my main function it is empty. Seconds after it is filled.
      Here is my connection and code:

      void DevicesManagement::processReceivedFrames()
      {
          if(!m_device)
              return;
          while (m_device->framesAvailable()) {
              const QCanBusFrame frame = m_device->readFrame();
              m_receivedFrames.append(frame.payload().toHex());
              set_deviceProfile(frame.payload().toHex().right(8).toUpper());
          }
          emit processOver();
          qDebug()<<get_deviceProfile();
          disconnect();
      }
      

      And i connected my slot to the signal like this:

          connect(m_device, &QCanBusDevice::framesReceived, this, &DevicesManagement::processReceivedFrames);
      
      

      And there is my main

      int main (int argc, char * argv []) {
          QGuiApplication app (argc, argv);
          QQmlApplicationEngine engine (&app);
          registerQtQmlTricksUiElements (&engine);
          registerQtQmlTricksSmartDataModel (&engine);
          DevicesManagement *manager=new DevicesManagement(&engine);
          QList<QObject*>mDeviceList;
          manager->initializeDevices("125000");
          manager->deviceProfileProcess();
          qDebug()<<manager->get_deviceProfile();
       
      
          engine.load (QUrl ("qrc:///splash_power.qml"));
      
          return app.exec ();
      }
      
      

      The problem is that my slot processReceivedFrames is not over yet when i read my frame. Therefore it is empty.
      Anyone got an idea to solve that issue?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Apr 2019, 08:36 last edited by
      #2

      @Babs said in Wait until slot end to continue program execution:

      qDebug()<<manager->get_deviceProfile();

      Do you mean it is empty here?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply 17 Apr 2019, 08:39
      0
      • J jsulm
        17 Apr 2019, 08:36

        @Babs said in Wait until slot end to continue program execution:

        qDebug()<<manager->get_deviceProfile();

        Do you mean it is empty here?

        B Offline
        B Offline
        Babs
        wrote on 17 Apr 2019, 08:39 last edited by
        #3

        @jsulm yes and it is filled when my slot ends

        J 1 Reply Last reply 17 Apr 2019, 08:42
        0
        • B Babs
          17 Apr 2019, 08:39

          @jsulm yes and it is filled when my slot ends

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 17 Apr 2019, 08:42 last edited by
          #4

          @Babs But why do you need to read it there?
          Correct way would be to emit a signal from DevicesManagement when the list is complete and read it in the slot.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 17 Apr 2019, 08:45
          2
          • J jsulm
            17 Apr 2019, 08:42

            @Babs But why do you need to read it there?
            Correct way would be to emit a signal from DevicesManagement when the list is complete and read it in the slot.

            B Offline
            B Offline
            Babs
            wrote on 17 Apr 2019, 08:45 last edited by
            #5

            @jsulm I use it to create an Object that i will expose to qml

            B 1 Reply Last reply 17 Apr 2019, 08:47
            0
            • B Babs
              17 Apr 2019, 08:45

              @jsulm I use it to create an Object that i will expose to qml

              B Offline
              B Offline
              Babs
              wrote on 17 Apr 2019, 08:47 last edited by
              #6

              @Babs here is what i do next in my main

              int main (int argc, char * argv []) {
                  QGuiApplication app (argc, argv);
                  QQmlApplicationEngine engine (&app);
                  registerQtQmlTricksUiElements (&engine);
                  registerQtQmlTricksSmartDataModel (&engine);
                  DevicesManagement *manager=new DevicesManagement(&engine);
                  QList<QObject*>mDeviceList;
                  manager->initializeDevices("125000");
                  manager->deviceProfileProcess();
                  qDebug()<<manager->get_deviceProfile();
                  CanOpenDevice *dev1=new CanOpenDevice(23,manager->get_deviceProfile(),&engine);
                  mDeviceList.append(dev1);
                  qmlRegisterType<CanOpenDevice>("com.device",1,0,"CanDevice");
                  engine.rootContext()->setContextProperty("mManager",manager);
                  engine.rootContext()->setContextProperty("mModel",QVariant::fromValue(mDeviceList));
              
              
                  engine.load (QUrl ("qrc:///splash_power.qml"));
              
                  return app.exec ();
              }
              
              J J 2 Replies Last reply 17 Apr 2019, 08:50
              0
              • B Babs
                17 Apr 2019, 08:47

                @Babs here is what i do next in my main

                int main (int argc, char * argv []) {
                    QGuiApplication app (argc, argv);
                    QQmlApplicationEngine engine (&app);
                    registerQtQmlTricksUiElements (&engine);
                    registerQtQmlTricksSmartDataModel (&engine);
                    DevicesManagement *manager=new DevicesManagement(&engine);
                    QList<QObject*>mDeviceList;
                    manager->initializeDevices("125000");
                    manager->deviceProfileProcess();
                    qDebug()<<manager->get_deviceProfile();
                    CanOpenDevice *dev1=new CanOpenDevice(23,manager->get_deviceProfile(),&engine);
                    mDeviceList.append(dev1);
                    qmlRegisterType<CanOpenDevice>("com.device",1,0,"CanDevice");
                    engine.rootContext()->setContextProperty("mManager",manager);
                    engine.rootContext()->setContextProperty("mModel",QVariant::fromValue(mDeviceList));
                
                
                    engine.load (QUrl ("qrc:///splash_power.qml"));
                
                    return app.exec ();
                }
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 17 Apr 2019, 08:50 last edited by
                #7

                @Babs You're trying to program synchronously using an asynchronous framework. You really should do it in an asynchronous way. Using signals/slots. Connect a slot in CanOpenDevice to a signal in DevicesManagement which is emitted when the list is filled.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                B 1 Reply Last reply 17 Apr 2019, 08:56
                3
                • J jsulm
                  17 Apr 2019, 08:50

                  @Babs You're trying to program synchronously using an asynchronous framework. You really should do it in an asynchronous way. Using signals/slots. Connect a slot in CanOpenDevice to a signal in DevicesManagement which is emitted when the list is filled.

                  B Offline
                  B Offline
                  Babs
                  wrote on 17 Apr 2019, 08:56 last edited by
                  #8

                  @jsulm thank you i'll try to do so. I thought i could may be suspend my main thread until my variable is filled

                  1 Reply Last reply
                  0
                  • B Babs
                    17 Apr 2019, 08:47

                    @Babs here is what i do next in my main

                    int main (int argc, char * argv []) {
                        QGuiApplication app (argc, argv);
                        QQmlApplicationEngine engine (&app);
                        registerQtQmlTricksUiElements (&engine);
                        registerQtQmlTricksSmartDataModel (&engine);
                        DevicesManagement *manager=new DevicesManagement(&engine);
                        QList<QObject*>mDeviceList;
                        manager->initializeDevices("125000");
                        manager->deviceProfileProcess();
                        qDebug()<<manager->get_deviceProfile();
                        CanOpenDevice *dev1=new CanOpenDevice(23,manager->get_deviceProfile(),&engine);
                        mDeviceList.append(dev1);
                        qmlRegisterType<CanOpenDevice>("com.device",1,0,"CanDevice");
                        engine.rootContext()->setContextProperty("mManager",manager);
                        engine.rootContext()->setContextProperty("mModel",QVariant::fromValue(mDeviceList));
                    
                    
                        engine.load (QUrl ("qrc:///splash_power.qml"));
                    
                        return app.exec ();
                    }
                    
                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 17 Apr 2019, 09:04 last edited by
                    #9

                    @Babs

                    lambdas are your friend ;-)

                    QObject::connect(manager, &DevicesManagement::devicesReady, &engine, [&engine, & mDeviceList](QStringList list)->void{
                          qDebug()<< list;
                        CanOpenDevice *dev1=new CanOpenDevice(23, list,&engine);
                        mDeviceList.append(dev1);
                        engine.rootContext()->setContextProperty("mManager",manager);
                        engine.rootContext()->setContextProperty("mModel",QVariant::fromValue(mDeviceList));     
                    });
                    engine.load (QUrl ("qrc:///splash_power.qml"));
                    
                        return app.exec ();
                    

                    should work, fine.

                    But it's untested.


                    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.

                    B 1 Reply Last reply 17 Apr 2019, 09:21
                    6
                    • J J.Hilk
                      17 Apr 2019, 09:04

                      @Babs

                      lambdas are your friend ;-)

                      QObject::connect(manager, &DevicesManagement::devicesReady, &engine, [&engine, & mDeviceList](QStringList list)->void{
                            qDebug()<< list;
                          CanOpenDevice *dev1=new CanOpenDevice(23, list,&engine);
                          mDeviceList.append(dev1);
                          engine.rootContext()->setContextProperty("mManager",manager);
                          engine.rootContext()->setContextProperty("mModel",QVariant::fromValue(mDeviceList));     
                      });
                      engine.load (QUrl ("qrc:///splash_power.qml"));
                      
                          return app.exec ();
                      

                      should work, fine.

                      But it's untested.

                      B Offline
                      B Offline
                      Babs
                      wrote on 17 Apr 2019, 09:21 last edited by
                      #10

                      @J.Hilk Thanks you. Its works.
                      Regards.

                      1 Reply Last reply
                      0

                      5/10

                      17 Apr 2019, 08:45

                      • Login

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