Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Error in function argument libusb, works in codeblock but no in class at Qt

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
2 Posts 2 Posters 1.8k Views 1 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.
  • A Offline
    A Offline
    aurquiel
    wrote on last edited by aurquiel
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

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