Qt DBus connect not working with typedef in SLOT arguments

641 Views Asked by At

I can connect DBus signals to Qt slots using QDBusConnection::connect and the old-style SLOT syntax:

#pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wconversion"
#   include <QCoreApplication>
#   include <QDBusConnection>
#   include <QDBusMetaType>
#   include <QDebug>
#   include <QMap>
#   include <QString>
#   include <QVariant>
#pragma GCC diagnostic pop

using PropertyDictionary = QMap<QString, QVariant>;
Q_DECLARE_METATYPE(PropertyDictionary)

class MyLogger : public QObject
{
    Q_OBJECT

public slots:

    void log(QString const& interface, QMap<QString, QVariant> const& properties)
    {
        logTypedef(interface, properties);
    }

    void logTypedef(QString const& interface, PropertyDictionary const& properties)
    {
        qDebug() << "interface = " << interface;
        qDebug() << "properties = " << properties;
    }
};

int main(int argc, char* argv[])
{
    qDBusRegisterMetaType<PropertyDictionary>();

    QCoreApplication app{argc, argv};
    MyLogger logger{};
    auto const connected
    {
        QDBusConnection::systemBus().connect
        (
            "org.freedesktop.timedate1",
            "/org/freedesktop/timedate1",
            "org.freedesktop.DBus.Properties",
            "PropertiesChanged",
            &logger,
            // SLOT(log(QString, QMap<QString, QVariant>)) // works
            SLOT(logTypedef(QString, PropertyDictionary)) // assert fails
        )
    };
    Q_ASSERT(connected);

    return app.exec();
}

#include "DBusTimezoneApp.moc"

The connect call succeeds and DBus signals are received if I use a QMap<QString, QVariant> argument for the slot. It fails if I use the PropertyDictionary typedef.

I know C++, but I'm new to the moc magic, so I hope for your help.

I'm building on Ubuntu 18.04, Qt 5.9.5 from the Ubuntu repo, using

/usr/lib/qt5/bin/moc  -I /usr/include/x86_64-linux-gnu/qt5 -I /usr/include/x86_64-linux-gnu/qt5/QtCore -I /usr/include/x86_64-linux-gnu/qt5/QtDBus -I /usr/include/x86_64-linux-gnu/qt5/QtNetwork  -i DBusTimezoneApp.cxx -o DBusTimezoneApp.moc
g++ -Wall -Wextra -Wconversion -Werror -pedantic -I /usr/include/x86_64-linux-gnu/qt5 -I /usr/include/x86_64-linux-gnu/qt5/QtCore -I /usr/include/x86_64-linux-gnu/qt5/QtDBus -I /usr/include/x86_64-linux-gnu/qt5/QtNetwork   -std=c++17 -fPIC -O0 -g3 -c DBusTimezoneApp.cxx
g++ -o DBusTimezoneApp DBusTimezoneApp.o -l Qt5Core -l Qt5DBus -l Qt5Network
1

There are 1 best solutions below

0
On

You have to register PropertyDictionary using qRegisterMetaType:

// ...
qDBusRegisterMetaType<PropertyDictionary>();
qRegisterMetaType<PropertyDictionary>("PropertyDictionary");
// ...