QDBusInterface is able to connect to org.freedesktop.DBus, but its status is invalid

Bug #1776446 reported by Dmitry
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
qtbase-opensource-src (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Consider the following example:

#include <QtCore>
#include <QtDBus>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QDBusInterface dbus("org.freedesktop.DBus",
                        "/org/freedesktop/DBus",
                        "org.freedesktop.DBus",
                        QDBusConnection::sessionBus());

    if (!dbus.isValid())
    {
        qDebug() << "IFACE IS NOT VALID" << dbus.lastError().message();
        return 1;
    }

    QDBusReply<QStringList> servicesReply = dbus.call("ListNames");

    if (!servicesReply.isValid())
    {
        qDebug() << "REPLY IS NOT VALID" << servicesReply.error().message();
        return 2;
    }

    qDebug() << servicesReply.value();
}

Compile and run. It prints

IFACE IS NOT VALID ""

The issue here is that QDBusInterface::isValid() returns false, however it's still possible to call its methods. If I comment out the "if (!dbus.isValid())" line, it starts to work fine:

#include <QtCore>
#include <QtDBus>

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    QDBusInterface dbus("org.freedesktop.DBus",
                        "/org/freedesktop/DBus",
                        "org.freedesktop.DBus",
                        QDBusConnection::sessionBus());
/*
    if (!dbus.isValid())
    {
        qDebug() << "IFACE IS NOT VALID" << dbus.lastError().message();
        return 1;
    }
*/
    // WORKS FINE!
    QDBusReply<QStringList> servicesReply = dbus.call("ListNames");

    if (!servicesReply.isValid())
    {
        qDebug() << "REPLY IS NOT VALID" << servicesReply.error().message();
        return 2;
    }

    qDebug() << servicesReply.value();
}

In Ubuntu 16.04 it worked fine. All updates have been installed.

Tags: bionic
Revision history for this message
Dmitry (dmitrymq) wrote :
description: updated
Revision history for this message
Dmitry Shachnev (mitya57) wrote :

I think you should not try to interact with org.freedesktop.DBus interface directly, instead you should use QDBusConnectionInterface for that. In your particular case, this would be using https://doc.qt.io/qt-5/qdbusconnectioninterface.html#registeredServiceNames-prop:

const QDBusConnectionInterface *iface = QDBusConnection::sessionBus().interface();
QDBusReply<QStringList> servicesReply = iface->registeredServiceNames();
qDebug() << servicesReply.value();

Any other interface would work fine, org.freedesktop.dbus is blacklisted here: https://code.qt.io/cgit/qt/qtbase.git/tree/src/dbus/qdbusintegrator.cpp?h=5.9#n2448

Changed in qtbase-opensource-src (Ubuntu):
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.