gpt4 book ai didi

c++ - Qt DBus连接不适用于SLOT参数中的typedef

转载 作者:行者123 更新时间:2023-12-03 07:03:51 25 4
gpt4 key购买 nike

我可以使用 QDBusConnection::connect 和旧式 SLOT 语法将DBus信号连接到Qt插槽:

#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"

如果我为插槽使用 connect参数,则 QMap<QString, QVariant>调用成功,并且接收到DBus信号。如果我使用 PropertyDictionary typedef,它将失败。

我知道C++,但是我对 moc魔术并不熟悉,所以希望对您有所帮助。

我正在使用Ubuntu repo的Ubuntu 18.04,Qt 5.9.5进行构建,使用
/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

最佳答案

您必须使用 qRegisterMetaType 注册PropertyDictionary:

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

关于c++ - Qt DBus连接不适用于SLOT参数中的typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com