Error in function argument libusb, works in codeblock but no in class at Qt



  • I have an example for hotplug USB that works in codeblocks, but when i tried to ported the functions to a class in qt i got a invalid parameter in a function.

    Every time that i disconnect the device with the vendorid=1256 productid=26720 the count variable set value 2 and the program exit, you can define your own vendorid and productid. If the device is plug in the program waits until unplog.

    Codeblocks y link the compiler to -lusb-1.0

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <libusb-1.0/libusb.h>
    
    static int count = 0;
    int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
                         libusb_hotplug_event event, void *user_data) {
      static libusb_device_handle *handle = NULL;
      struct libusb_device_descriptor desc;
      int rc;
      (void)libusb_get_device_descriptor(dev, &desc);
      if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
        rc = libusb_open(dev, &handle);
        if (LIBUSB_SUCCESS != rc) {
          printf("Could not open USB device\n");
        }
      } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
        if (handle) {
          libusb_close(handle);
          handle = NULL;
        }
      } else {
        printf("Unhandled event %d\n", event);
      }
      count++;
      return 0;
    }
    int main (void) {
      libusb_hotplug_callback_handle handle;
      int rc;
      libusb_init(NULL);
      rc = libusb_hotplug_register_callback(  NULL, (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
     LIBUSB_HOTPLUG_ENUMERATE,
                                               1256,
                                               26720,
                                                LIBUSB_HOTPLUG_MATCH_ANY, 
                                                hotplug_callback,
                                                NULL,
                                                &handle);
      if (LIBUSB_SUCCESS != rc) {
        printf("Error creating a hotplug callback\n");
        libusb_exit(NULL);
        return EXIT_FAILURE;
      }
      while (count < 2) {
        libusb_handle_events_completed(NULL, NULL);
        usleep(10000);
      }
      libusb_hotplug_deregister_callback(NULL, handle);
      libusb_exit(NULL);
      return 0;
    }
    

    That works great in a console.

    Then moving to a QT class

    header

    #ifndef BACKEND_H
    #define BACKEND_H
    #include <QObject>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <libusb-1.0/libusb.h> //Include libsub
    
    class backend: public QObject
    {
        Q_OBJECT
    public:
        backend();
    
        libusb_hotplug_callback_handle handle;
    
        int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
                             libusb_hotplug_event event, void *user_data);
    
    
        static int count;
        void hot();
    };
    
    
    #endif // BACKEND_H
    

    **body **

    #include "backend.h"
    
    backend::backend()
    {
    
    }
    
    int backend::count=0;
    
    void backend::hot()
    {
        int rc;
        libusb_init(NULL);
        rc = libusb_hotplug_register_callback(  NULL, (libusb_hotplug_event) (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
                                                LIBUSB_HOTPLUG_ENUMERATE,
                                                1256,
                                                26720,
                                                LIBUSB_HOTPLUG_MATCH_ANY,
                                                hotplug_callback,
                                                NULL,
                                                &handle);
        if (LIBUSB_SUCCESS != rc) {
            libusb_exit(NULL);
        }
        while (count < 2) {
            libusb_handle_events_completed(NULL, NULL);
            usleep(10000);
        }
        libusb_hotplug_deregister_callback(NULL, handle);
        libusb_exit(NULL);
    }
    
    int backend::hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
                         libusb_hotplug_event event, void *user_data)
    {
      static libusb_device_handle *handle = NULL;
      struct libusb_device_descriptor desc;
      int rc;
      (void)libusb_get_device_descriptor(dev, &desc);
      if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
        rc = libusb_open(dev, &handle);
      } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
        if (handle) {
          libusb_close(handle);
          handle = NULL;
        }
      }
      count++;
      return 0;
    }
    
    

    At this point try to compile and get

    /home/zen/QT/hotplug_test/backend.cpp:21: error: invalid use of non-static member function
    &handle);
    ^

    I have a problem with handel argument but i don't know why


  • Lifetime Qt Champion

    Hi,

    Because you are trying to pass a member variable as a callback. You can't, your callback should be a free function.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.