我想通过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";
}
以下是发件人程序的输出:
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? Receiver似乎只看到All 0,并且从未收到具有有效值的协议缓冲区对象。
我的编组代码有问题吗?还有什么可以继续?
答案 0 :(得分:1)
为了使这项工作,实现像这样的运算符:
// PROTOBUF-MODIFICATION-DBUS
// Marshall the companyData data into a D-Bus argument
QDBusArgument &operator<<(QDBusArgument &argument, const companyData &dataToWrite)
{
argument.beginStructure();
// Break out the various properties of dataToWrite protocol buffer
int name = dataToWrite.name();
int id = dataToWrite.id();
int email = dataToWrite.email();
argument << name;
argument << id;
argument << email;
argument.endStructure();
return argument;
}
// PROTOBUF-MODIFICATION-DBUS
// Retrieve the companyData data from the D-Bus argument
const QDBusArgument &operator>>(const QDBusArgument &argument, companyData &dataToWrite)
{
int name, id, email;
argument.beginStructure();
argument >> name;
argument >> id;
argument >> email;
argument.endStructure();
dataToWrite.set_name(name);
dataToWrite.set_id(id);
dataToWrite.set_email(email);
return argument;
}