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

Connect signals with parameters to lambda functions

Scheduled Pinned Locked Moved Solved General and Desktop
signalslotlambda
7 Posts 2 Posters 6.9k 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.
  • B Offline
    B Offline
    beecksche
    wrote on 9 May 2016, 09:43 last edited by beecksche 5 Oct 2016, 07:33
    #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!

    K 1 Reply Last reply 9 May 2016, 09:53
    0
    • B beecksche
      9 May 2016, 09:43

      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!

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 9 May 2016, 09:53 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
      • B Offline
        B Offline
        beecksche
        wrote on 9 May 2016, 09:59 last edited by
        #3

        @kshegunov

        Works perfectly, thanks a lot!

        K 1 Reply Last reply 9 May 2016, 10:03
        0
        • B beecksche
          9 May 2016, 09:59

          @kshegunov

          Works perfectly, thanks a lot!

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 9 May 2016, 10:03 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
          • B Offline
            B Offline
            beecksche
            wrote on 9 May 2016, 10:15 last edited by beecksche 5 Sept 2016, 11:53
            #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.

            K 1 Reply Last reply 9 May 2016, 16:19
            1
            • B beecksche
              9 May 2016, 10:15

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

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 9 May 2016, 16:19 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

              B 1 Reply Last reply 10 May 2016, 07:14
              0
              • K kshegunov
                9 May 2016, 16:19

                @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

                B Offline
                B Offline
                beecksche
                wrote on 10 May 2016, 07:14 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

                3/7

                9 May 2016, 09:59

                • Login

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