PDA

View Full Version : USB detection using QDBusConnection API



USER8
12th October 2013, 07:47
I'm using QDBusConnecton API to detect USB.
The code is:

mainwindow.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QDBusConnection>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!QDBusConnection::systemBus().isConnected()) {
qDebug() << "Cannot connect to system bus";
}

bool connected = QDBusConnection::systemBus().connect(
"org.freedesktop.UDisks",
"/org/freedesktop/UDisks",
"org.freedesktop.UDisks",
"DeviceAdded",
this,SLOT(deviceAdded(QDBusObjectPath)));
}


MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::deviceAdded(QDBusObjectPath dev)
{
qDebug() << "device added!"<<dev.path();
}




mainwindow .h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui/QApplication>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow {
Q_OBJECT


public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();



private:
Ui::MainWindow *ui;

private slots:
void deviceAdded(QDBusObjectPath dev);


};


#endif // MAINWINDOW_H


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?

aamer4yu
12th October 2013, 09:59
How did you generate the ui_mainwindow.h ??

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

USER8
12th October 2013, 10:22
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?

anda_skoa
12th October 2013, 11:09
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,
_

USER8
15th October 2013, 07:22
I have made modifications and using the following code:



#include <QtCore/QDebug>
#include <QtGui/QApplication>
#include <QtDBus/QDBusConnection>

#define HAL_SERV "org.freedesktop.Hal"
#define HAL_MGR_INT "org.freedesktop.Hal.Manager"
#define HAL_DEV_INT "org.freedesktop.Hal.Device"

#define HAL_MGR_PATH "/org/freedesktop/Hal/Manager"
#define HAL_DEVS_PATH "/org/freedesktop/Hal/devices"

class Hal : public QObject
{
Q_OBJECT;

public:
Hal() :
QObject(),
cnx( QDBusConnection::connectToBus( QDBusConnection::SystemBus, "system" ) )
{
cnx.connect(
HAL_SERV, HAL_MGR_PATH, HAL_MGR_INT, "DeviceAdded",
this, SLOT(added(QString)) );
cnx.connect(
HAL_SERV, HAL_MGR_PATH, HAL_MGR_INT, "DeviceRemoved",
this, SLOT(removed(QString)) );
}

private slots:
void added( QString dev )
{
qDebug() << __FUNCTION__ << dev;
}

void removed( QString dev )
{
qDebug() << __FUNCTION__ << dev;
}

private:
QDBusConnection cnx;
};

int main( int ac, char * * av )
{
QApplication app( ac, av );

Hal hal;

return( app.exec() );
}

#include "main.moc"


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?

aamer4yu
15th October 2013, 09:26
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.

USER8
15th October 2013, 10:35
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?

anda_skoa
15th October 2013, 16:42
Have you checked the return values of your connect() calls?
Does your system run the HAL service?

Cheers,
_

USER8
16th October 2013, 13:20
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.

anda_skoa
16th October 2013, 18:22
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',interfa ce='org.freedesktop.Hal.Manager'"

Cheers,
_