Using std::vector<uint8_t> with QtDBus without copying it to QByteArray

时间:2018-03-09 19:07:38

标签: c++ qt dbus

I'd like to use std::vector<uint8_t> with QtDBus:

Q_DECLARE_METATYPE (std::vector<uchar>);
[...]
std::vector<uint8_t> v;
QVariant variant;
variant.setValue(v);
QDBusReply<std::vector<uint8_t>> reply = interface.call("SomeDBusFunction", variant);

A runtime error occurs:

QDBusMarshaller: type `std::vector<uchar>' (1044) is not registered with D-BUS. Use qDBusRegisterMetaType to register it
QDBusConnection: error: could not send message to service "com.some.url" path "/com/some/url" interface "com.some.url.object" member "SomeDBusFunction": Marshalling failed: Unregistered type std::vector<uchar> passed in arguments

So I'm registering the type and adding missing operator definitions, otherwise it won't compile:

qDBusRegisterMetaType<std::vector<uchar>>();
[...]
QDBusArgument &operator<<(QDBusArgument &argument, const std::vector<uchar> &v)
{
    argument.beginArray(qMetaTypeId<uchar>());
    for (size_t i = 0; i < v.size(); ++i)
    {
        argument << v[i];
    }
    argument.endArray();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, std::vector<uchar> &v)
{
    argument.beginArray();
    v.clear();
    while (!argument.atEnd())
    {
        uchar element;
        argument >> element;
        v.push_back( element );
    }
    argument.endArray();
    return argument;
}

However then there is another error:

QDBusMarshaller: type `std::vector<uchar>' attempts to redefine basic D-BUS type 'ay' (QByteArray) (Did you forget to call beginStructure() ?)
QDBusMarshaller::appendVariantInternal: Found unknown D-BUS type ''

Is it possible to use std::vector<uint8_t> with QtDBus without copying it to QByteArray as it causes one additional copy operation?

0 个答案:

没有答案