我想通过 Qt DBUS API 发送一个自定义 C++ 类。我已经使用 protoc 编译器从 .proto 文件创建了类,并将它们添加到我在 QtCreator 中的项目中。现在我想验证我是否可以通过 dbus API 将自定义类作为 QVariant 发送。我有一个接收器和发送器程序,可以发送一个简单的测试字符串,以便 Dbus 工作。将协议缓冲区类添加为元类型后,我无法发送它。
我的测试 .proto 文件只包含整数:
message MyData {
required int32 name = 1;
required int32 id = 2;
optional int32 email = 3;
}
在我添加的协议缓冲区类头文件中:
#include <QMetaType>
#include <QDBusMetaType>
...
friend QDBusArgument &operator<<(QDBusArgument &argument, const MyData &dataToWrite);
friend const QDBusArgument &operator>>(const QDBusArgument &argument, MyData &dataToWrite);
...
Q_DECLARE_METATYPE(MyData )
在我添加的协议缓冲区类实现文件中:
#include <QDebug>
...
#include <QMetaType>
#include <QDBusMetaType>
// Marshall the MyData data into a D-Bus argument
QDBusArgument &operator<<(QDBusArgument &argument, const MyData &dataToWrite)
{
qDebug() << "OPERATOR<<";
argument.beginStructure();
// Break out the various properties of dataToWrite protocol buffer
int name = dataToWrite.name();
int id = dataToWrite.id();
int email = dataToWrite.email();
qDebug() << name;
qDebug() << id;
qDebug() << email;
argument << name;
argument << id;
argument << email;
argument.endStructure();
return argument;
}
// Retrieve the MyData data from the D-Bus argument
const QDBusArgument &operator>>(const QDBusArgument &argument, MyData &dataToWrite)
{
qDebug() << "OPERATOR>>";
argument.beginStructure();
// Break out the various properties of dataToWrite protocol buffer
int name = dataToWrite.name();
int id = dataToWrite.id();
int email = dataToWrite.email();
qDebug() << name;
qDebug() << id;
qDebug() << email;
argument >> name;
argument >> id;
argument >> email;
argument.endStructure();
return argument;
}
主要看起来像这样:
QCoreApplication a(argc, argv);
dbussender* client = new dbussender("com.one.two.three.nvram", "/dbusReadWriteNvRam", QDBusConnection::sessionBus(), 0);
// Create a protocol buffer class and provide its properties with values
MyData dataToWrite;
dataToWrite.set_name(2);
dataToWrite.set_id(3);
dataToWrite.set_email(4);
QString command3 = "Contacting Protobuf Receiver and calling WRITENVRAM...";
QString response3 = client->writeNVRam(dataToWrite);
std::cout << "Command: " << command3.toStdString() << std::endl;
std::cout << "Response: " << response3.toStdString() << std::endl;
我的 dbussender 类调用远程函数是这样的:
inline QDBusPendingReply<QString> writeNVRam(MyData dataToWrite)
{
qDebug() << "Sending " << dataToWrite.name();
qDebug() << "Sending " << dataToWrite.id();
qDebug() << "Sending " << dataToWrite.email();
QList<QVariant> argumentList;
argumentList << QVariant::fromValue<MyData>(dataToWrite);
return asyncCallWithArgumentList(QLatin1String("writeNVRam"), argumentList);
}
最终在我的接收程序中,这个函数被调用但总是返回 0:
// Write NVRAM
QString dbusReadWriteNvRam::writeNVRam(MyData dataToWrite) {
qDebug() << "WRITE NVRAM COMMAND CALLED";
qDebug() << "Unpacking: " << dataToWrite.name();
qDebug() << "Unpacking: " << dataToWrite.id();
qDebug() << "Unpacking: " << dataToWrite.email();
return "HELLO CLASS";
}
这是 Sender 程序的输出:
Sending 2
Sending 3
Sending 4
OPERATOR<<
0
0
0
OPERATOR<<
2
3
4
Command: Contacting Protobuf Receiver and calling WRITENVRAM...
Response: HELLO CLASS
这是 Receiver 程序的输出:
OPERATOR<<
0
0
0
OPERATOR>>
0
0
0
WRITE NVRAM COMMAND CALLED
Unpacking: 0
Unpacking: 0
Unpacking: 0
为什么编组函数似乎被调用了两次?为什么第二次调用似乎包含我的协议缓冲区的 3 个属性的有效值 2、3、4,但第一次调用全是 0?接收器似乎只看到全 0 并且从不接收具有有效值的协议缓冲区对象。
我的编组代码有问题吗?还有什么可能发生的?