Results 1 to 10 of 10

Thread: USB detection using QDBusConnection API

  1. #1
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default USB detection using QDBusConnection API

    I'm using QDBusConnecton API to detect USB.
    The code is:

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include "ui_mainwindow.h"
    4.  
    5. #include <QDBusConnection>
    6. #include <QDebug>
    7.  
    8. MainWindow::MainWindow(QWidget *parent)
    9. : QMainWindow(parent), ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. if (!QDBusConnection::systemBus().isConnected()) {
    13. qDebug() << "Cannot connect to system bus";
    14. }
    15.  
    16. bool connected = QDBusConnection::systemBus().connect(
    17. "org.freedesktop.UDisks",
    18. "/org/freedesktop/UDisks",
    19. "org.freedesktop.UDisks",
    20. "DeviceAdded",
    21. this,SLOT(deviceAdded(QDBusObjectPath)));
    22. }
    23.  
    24.  
    25. MainWindow::~MainWindow()
    26. {
    27. delete ui;
    28. }
    29.  
    30. void MainWindow::deviceAdded(QDBusObjectPath dev)
    31. {
    32. qDebug() << "device added!"<<dev.path();
    33. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow .h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtGui/QApplication>
    6.  
    7. namespace Ui {
    8. class MainWindow;
    9. }
    10.  
    11. class MainWindow : public QMainWindow {
    12. Q_OBJECT
    13.  
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19.  
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23.  
    24. private slots:
    25. void deviceAdded(QDBusObjectPath dev);
    26.  
    27.  
    28. };
    29.  
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    I'm getting error as:

    mainwindow.cpp:3:27: fatal error: ui_mainwindow.h: No such file or directory
    compilation terminated.

    Please tell me is there any mistake in this code?
    how to solve this Issue?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: USB detection using QDBusConnection API

    How did you generate the ui_mainwindow.h ??

    Check the spelling and make sure it is the same file.

  3. #3
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: USB detection using QDBusConnection API

    My application is not generating ui_mainwindow.h file.
    How to generate that file?
    Is there any path should I need to include in .pro file to generate ui_mainwindow.h file?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: USB detection using QDBusConnection API

    A ui_something.h file is the result of user interface code generated by UIC from a .ui file (qt Designer file) with the same name.
    This UI file has to be listed in the FORMS variable in the .pro file.

    If you don't have a .ui file I wonder how you got the include and the code using it in the first place.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: USB detection using QDBusConnection API

    I have made modifications and using the following code:

    Qt Code:
    1. #include <QtCore/QDebug>
    2. #include <QtGui/QApplication>
    3. #include <QtDBus/QDBusConnection>
    4.  
    5. #define HAL_SERV "org.freedesktop.Hal"
    6. #define HAL_MGR_INT "org.freedesktop.Hal.Manager"
    7. #define HAL_DEV_INT "org.freedesktop.Hal.Device"
    8.  
    9. #define HAL_MGR_PATH "/org/freedesktop/Hal/Manager"
    10. #define HAL_DEVS_PATH "/org/freedesktop/Hal/devices"
    11.  
    12. class Hal : public QObject
    13. {
    14. Q_OBJECT;
    15.  
    16. public:
    17. Hal() :
    18. cnx( QDBusConnection::connectToBus( QDBusConnection::SystemBus, "system" ) )
    19. {
    20. cnx.connect(
    21. HAL_SERV, HAL_MGR_PATH, HAL_MGR_INT, "DeviceAdded",
    22. this, SLOT(added(QString)) );
    23. cnx.connect(
    24. HAL_SERV, HAL_MGR_PATH, HAL_MGR_INT, "DeviceRemoved",
    25. this, SLOT(removed(QString)) );
    26. }
    27.  
    28. private slots:
    29. void added( QString dev )
    30. {
    31. qDebug() << __FUNCTION__ << dev;
    32. }
    33.  
    34. void removed( QString dev )
    35. {
    36. qDebug() << __FUNCTION__ << dev;
    37. }
    38.  
    39. private:
    40. };
    41.  
    42. int main( int ac, char * * av )
    43. {
    44. QApplication app( ac, av );
    45.  
    46. Hal hal;
    47.  
    48. return( app.exec() );
    49. }
    50.  
    51. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    By using above code,connection is successful,but it not receiving the signal even USB connected to the system.Anyone can help me regarding this?
    Last edited by USER8; 15th October 2013 at 07:26.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: USB detection using QDBusConnection API

    Try attaching and removing the usb at runtime. The signals are meant for that.
    If the usb is already attached to the system , I am afraid that the signal will be emitted when your app connects to it.

    You will probably need to query some function for the list of usb devices connected at the start-up of your application.

  7. #7
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: USB detection using QDBusConnection API

    I have tried by connecting and removing USB, even though Not detecting the USB,not receiving the signals.
    It should detect any devices right? any mistakes I did?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: USB detection using QDBusConnection API

    Have you checked the return values of your connect() calls?
    Does your system run the HAL service?

    Cheers,
    _

  9. #9
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: USB detection using QDBusConnection API

    yes the bus connect function returning true,but signal is not emitting.

    and How to check our system is able to service HAL?
    Is there I need to install any packages regarding this?
    Right now ubuntu has hal-info 20091130-1.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: USB detection using QDBusConnection API

    The connects would likely fail if it were not running, but you can check yourself, e.g. by running
    qdbus --system
    to list all service registered with the system bus.

    You could also check if the service is actually emitting the signal, i.e. if the problem is on your side or not
    dbus-monitor --system "type='signal',sender='org.freedesktop.Hal',interf ace='org.freedesktop.Hal.Manager'"

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    USER8 (17th October 2013)

Similar Threads

  1. Edge-detection (maybe using QGL?)
    By mwgobetti in forum Newbie
    Replies: 6
    Last Post: 20th January 2012, 13:32
  2. collision detection...
    By Muffin in forum Newbie
    Replies: 1
    Last Post: 8th January 2010, 10:28
  3. Out of range detection
    By zgulser in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2009, 09:32
  4. Crash detection
    By ^NyAw^ in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2008, 12:09
  5. Mouse detection
    By eu.x in forum Newbie
    Replies: 3
    Last Post: 25th April 2007, 23:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.