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. Connect signals with parameters to lambda functions
QtWS25 Last Chance

Connect signals with parameters to lambda functions

Scheduled Pinned Locked Moved Solved General and Desktop
signalslotlambda
7 Posts 2 Posters 6.8k 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    Hi,
    i connect some singals to a lambda function/slot. Can i also use the parameter of the signal in the lambda functions?

    My current code:

    if (mbServer->state() != QModbusDevice::UnconnectedState)
    {
    	mbServer->disconnectDevice();
    
    	QEventLoop loop;
    	connect(mbServer, &QModbusDevice::stateChanged, this, [this, &loop]
    	{
    		if (mbServer->state() == QModbusDevice::UnconnectedState)
    			loop.quit();
    	});
    
    	loop.exec();
    }
    
    

    The signal QModbusDevice::stateChange has the parameter QModbusDevice::State state (http://doc.qt.io/qt-5/qmodbusdevice.html#stateChanged).

    Thanks!

    kshegunovK 1 Reply Last reply
    0
    • beeckscheB beecksche

      Hi,
      i connect some singals to a lambda function/slot. Can i also use the parameter of the signal in the lambda functions?

      My current code:

      if (mbServer->state() != QModbusDevice::UnconnectedState)
      {
      	mbServer->disconnectDevice();
      
      	QEventLoop loop;
      	connect(mbServer, &QModbusDevice::stateChanged, this, [this, &loop]
      	{
      		if (mbServer->state() == QModbusDevice::UnconnectedState)
      			loop.quit();
      	});
      
      	loop.exec();
      }
      
      

      The signal QModbusDevice::stateChange has the parameter QModbusDevice::State state (http://doc.qt.io/qt-5/qmodbusdevice.html#stateChanged).

      Thanks!

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @beecksche

      Can i also use the paramater of the signals in the lambda functions?

      You can, but your lambda should have parameters as well ... :)

      QEventLoop loop;
      
      auto lambda = [&loop] (QModbusDevice::State state) -> void
      {
          if (state == QModbusDevice::UnconnectedState)
              loop.quit();
      }
      
      connect(mbServer, &QModbusDevice::stateChanged, lambda);
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by
        #3

        @kshegunov

        Works perfectly, thanks a lot!

        kshegunovK 1 Reply Last reply
        0
        • beeckscheB beecksche

          @kshegunov

          Works perfectly, thanks a lot!

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @beecksche
          No problem. May I ask something? Is modbus communication synchronous, I didn't see any async API in the docs?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by beecksche
            #5

            @kshegunov
            Modbus clients have a asynchronous API (http://doc.qt.io/qt-5/qmodbusclient.html#details).

            For modbus servers it is not specified in the docs. But as far as i used the class, all calls are also asynchronous. In my case i want to ensure that server is not connected, before connect again. If i understood the docs of the QModbusDevice correctly, it is also possible to inherit from QModbusDevice and override the open() and close() function to ensure that.

            kshegunovK 1 Reply Last reply
            1
            • beeckscheB beecksche

              @kshegunov
              Modbus clients have a asynchronous API (http://doc.qt.io/qt-5/qmodbusclient.html#details).

              For modbus servers it is not specified in the docs. But as far as i used the class, all calls are also asynchronous. In my case i want to ensure that server is not connected, before connect again. If i understood the docs of the QModbusDevice correctly, it is also possible to inherit from QModbusDevice and override the open() and close() function to ensure that.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @beecksche said:

              Modbus clients have a asynchronous API

              Thanks. I skipped the description and went directly to the signals, but the way the API is organized is a bit different from QTcpSocket, so I got tangled up ... :D

              Read and abide by the Qt Code of Conduct

              beeckscheB 1 Reply Last reply
              0
              • kshegunovK kshegunov

                @beecksche said:

                Modbus clients have a asynchronous API

                Thanks. I skipped the description and went directly to the signals, but the way the API is organized is a bit different from QTcpSocket, so I got tangled up ... :D

                beeckscheB Offline
                beeckscheB Offline
                beecksche
                wrote on last edited by
                #7

                @kshegunov said:

                the way the API is organized is a bit different from QTcpSocket

                You're right. I was also a little bit confused, when i used the QSerialBus module the first time (see my topic https://forum.qt.io/topic/65572/qmodbusmaster-example).

                Like i said, i think you can built your own class by inherit from QModbusDevice to have a synchronous API. The QSerialBus module is marked as technical preview in Qt 5.6, maybe they add some new functions in future releases similar to QTcpSocket like waitForConnected etc.

                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