• 7717阅读
  • 4回复

[提问]为什么插入U盘的时候没有触发这个函数 [复制链接]

上一主题 下一主题
离线zzxap
 

只看楼主 倒序阅读 楼主  发表于: 2011-05-11
#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.Hal",
            "/org/freedesktop/Hal/Manager",
            "org.freedesktop.Hal.Manager",
            "DeviceAdded",
            this, SLOT(deviceAdded()));
}


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

void MainWindow::deviceAdded()
{
    qDebug() << "Device added";  //为什么插入U盘的时候没有触发这个函数
}
[ 此帖被zzxap在2011-05-11 18:02重新编辑 ]
离线cutemmll
只看该作者 1楼 发表于: 2011-05-12
bool QDBusConnection::connect ( const QString & service, const QString & path, const QString & interface, const QString & name, QObject * receiver, const char *slot )

Connects the signal specified by the service, path, interface and name parameters to the slot slot in object receiver. The arguments service and path can be empty, denoting a connection to any signal of the (interface, name) pair, from any remote application.
Returns true if the connection was successful.
Warning: The signal will only be delivered to the slot if the parameters match. This verification can be done only when the signal is received, not at connection time.


根据文档最后一行的描述可以知道,这个信号只有在槽的参数完全匹配是才会被递交给槽,而且在connecte时并不会去验证槽函数的参数是否与信号参数匹配。你检测的是硬件抽象层的信号,该信号中会附带一个QString类型Object的路径,所以你也必须在你的槽函数中加上一个QString参数。
改成下面这种方式
  1. QDBusConnection::systemBus().connect(    "org.freedesktop.Hal",
  2.                         "/org/freedesktop/Hal/Manager",
  3.                         "org.freedesktop.Hal.Manager",
  4.                         "DeviceAdded",
  5.                         this,
  6.                         SLOT(slotDeviceAdded(QString )));
  7. void MainWindow::slotDeviceAdded(QString path)
  8. {
  9.     qDebug()<<"something happened"<<path;
  10. }

详见
http://www.qtcn.org/bbs/read.php?tid=14535
[ 此帖被cutemmll在2011-05-12 11:11重新编辑 ]
c------------enjoy qt & enjoy life-----------++
离线zzxap

只看该作者 2楼 发表于: 2011-05-17
引用第1楼cutemmll于2011-05-12 11:10发表的  :

.......


这样还是触发不了
离线seven_1990
只看该作者 3楼 发表于: 2011-07-06
可是可以了 但是为什么插入U盘的时候调用的七次槽函数 拔出的时候也是调用七次  跪求解答
离线pipfeng
只看该作者 4楼 发表于: 2011-07-15
seven_1990是如何触发到的啊,我这里也是不能触发到。
快速回复
限100 字节
 
上一个 下一个