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
Forum Updated to NodeBB v4.3 + New Features

Wait until slot end to continue program execution

Scheduled Pinned Locked Moved Solved General and Desktop
qcanbusdevice
10 Posts 3 Posters 3.7k 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 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?

    jsulmJ 1 Reply Last reply
    0
    • B Babs

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • jsulmJ jsulm

        @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 last edited by
        #3

        @jsulm yes and it is filled when my slot ends

        jsulmJ 1 Reply Last reply
        0
        • B Babs

          @jsulm yes and it is filled when my slot ends

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          2
          • jsulmJ jsulm

            @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 last edited by
            #5

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

            B 1 Reply Last reply
            0
            • B Babs

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

              B Offline
              B Offline
              Babs
              wrote on 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 ();
              }
              
              jsulmJ J.HilkJ 2 Replies Last reply
              0
              • B Babs

                @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 ();
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                3
                • jsulmJ jsulm

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

                    @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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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
                    6
                    • J.HilkJ J.Hilk

                      @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 last edited by
                      #10

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

                      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