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. QtConcurrent and pointer to a class...this may be a simple c++ dereferencing question
Qt 6.11 is out! See what's new in the release blog

QtConcurrent and pointer to a class...this may be a simple c++ dereferencing question

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 800 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.
  • D Offline
    D Offline
    Daniel Williams
    wrote on last edited by aha_1980
    #1

    I have a class and method that is working fine. Now I want to make it concurrent and am having conceptual issue with QtConcurrent as I'm new to that part of Qt.

    Working method:

    void ConfiguredDevices::updateConfiguredDevicesState() {
        Device *device;
        DeviceListIterator iter(_persistedDevices);
        while (iter.hasNext()) {
           iter.next();
           device = iter.value();
           device->checkAvailability();
        }
    }
    

    The checkAvailability method tries to connect to the device and updates a property in the class with the availability. This can take some time, especially if it has to wait for a timeout when the device isn't available, so I wanted to make it concurrent. I was following the nice youtube tutorial from "voidrealms" (https://www.youtube.com/watch?v=tvpC8UrPpZ4) and was thinking I could do the following:

    void ConfiguredDevices::updateConfiguredDevicesState() {
        Device *device;
        DeviceListIterator iter(_persistedDevices);
        while (iter.hasNext()) {
           iter.next();
           device = iter.value();
           QtConcurrent::run(&(*device), &(device->checkAvailability));
        }
    }
    
    

    But, I get an error, "cannot create a non-constant pointer to a member function." Maybe I'm doing something wrong dereferencing the pointer and trying to cast it to a reference.

    Am I going down the right path? Is there another threading path I should consider? Thank you in advance for your help!

    CP71C 1 Reply Last reply
    0
    • CP71C CP71

      @Daniel-Williams If I have to do this I will try to write as follows:

      QtConcurrent::run( (Device*)device, Device::checkAvailability);

      D Offline
      D Offline
      Daniel Williams
      wrote on last edited by Daniel Williams
      #5

      @CP71 I changed to that syntax and get a different error: call to non-static member function without and object argument. However, when I changed to QtConcurrent::run((Device*)device, &Device::checkAvailability); I no longer get the error. I'll run the program to test it out.

      Thank you.

      1 Reply Last reply
      1
      • D Daniel Williams

        I have a class and method that is working fine. Now I want to make it concurrent and am having conceptual issue with QtConcurrent as I'm new to that part of Qt.

        Working method:

        void ConfiguredDevices::updateConfiguredDevicesState() {
            Device *device;
            DeviceListIterator iter(_persistedDevices);
            while (iter.hasNext()) {
               iter.next();
               device = iter.value();
               device->checkAvailability();
            }
        }
        

        The checkAvailability method tries to connect to the device and updates a property in the class with the availability. This can take some time, especially if it has to wait for a timeout when the device isn't available, so I wanted to make it concurrent. I was following the nice youtube tutorial from "voidrealms" (https://www.youtube.com/watch?v=tvpC8UrPpZ4) and was thinking I could do the following:

        void ConfiguredDevices::updateConfiguredDevicesState() {
            Device *device;
            DeviceListIterator iter(_persistedDevices);
            while (iter.hasNext()) {
               iter.next();
               device = iter.value();
               QtConcurrent::run(&(*device), &(device->checkAvailability));
            }
        }
        
        

        But, I get an error, "cannot create a non-constant pointer to a member function." Maybe I'm doing something wrong dereferencing the pointer and trying to cast it to a reference.

        Am I going down the right path? Is there another threading path I should consider? Thank you in advance for your help!

        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by
        #2

        @Daniel-Williams Hi,
        when I use QtConcurrent usually use the following syntax:

        QFuture cur = QtConcurrent::run( (myclass*)this, myclass::FunctionName);

        Note: this example is for a function without parameters.

        D 1 Reply Last reply
        0
        • CP71C CP71

          @Daniel-Williams Hi,
          when I use QtConcurrent usually use the following syntax:

          QFuture cur = QtConcurrent::run( (myclass*)this, myclass::FunctionName);

          Note: this example is for a function without parameters.

          D Offline
          D Offline
          Daniel Williams
          wrote on last edited by
          #3

          @CP71 Thank you.

          In this case I have a QList<Device *> _persistedDevices. I want to call the checkAvailability method on each object in a separate thread. The &(*device) seems to be okay as the first parameter. Getting a reference to the object method seems to be what's causing confusion.

          CP71C 1 Reply Last reply
          0
          • D Daniel Williams

            @CP71 Thank you.

            In this case I have a QList<Device *> _persistedDevices. I want to call the checkAvailability method on each object in a separate thread. The &(*device) seems to be okay as the first parameter. Getting a reference to the object method seems to be what's causing confusion.

            CP71C Offline
            CP71C Offline
            CP71
            wrote on last edited by
            #4

            @Daniel-Williams If I have to do this I will try to write as follows:

            QtConcurrent::run( (Device*)device, Device::checkAvailability);

            D 1 Reply Last reply
            1
            • CP71C CP71

              @Daniel-Williams If I have to do this I will try to write as follows:

              QtConcurrent::run( (Device*)device, Device::checkAvailability);

              D Offline
              D Offline
              Daniel Williams
              wrote on last edited by Daniel Williams
              #5

              @CP71 I changed to that syntax and get a different error: call to non-static member function without and object argument. However, when I changed to QtConcurrent::run((Device*)device, &Device::checkAvailability); I no longer get the error. I'll run the program to test it out.

              Thank you.

              1 Reply Last reply
              1

              • Login

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