本文介绍了PyQt5 dbus:强制信号参数的类型签名为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个MPRIS播放器,该播放器通过
dbus与客户进行通信。播放状态更改时,我需要发出信号。但是,
信号需要(sa {sv} as)的格式,而我的代码正在生成
(sa {sv} av)。这是重要的部分:

I'm writing an MPRIS player, which communicates with clients overdbus. I need to emit a signal when my playback state changes. However,the signal requires a format of (sa{sv}as), and my code is producing(sa{sv}av). Here's the important part:

self.signal = QDBusMessage.createSignal(
   "/org/mpris/MediaPlayer2",
   "org.freedesktop.DBus.Properties",
   "PropertiesChanged"
)
self.signal.setArguments(
    [interface, {property: values}, ['']]
)

问题是第三个给setArguments的列表中的项目。它是
列表中的空字符串,因为我需要产生一种类型的字符串数组
as ),但是pyqt5会将其转换为变体数组( av )。

The problem is the third item in the list given to setArguments. It isan empty string in a list because I need to produce a type of 'arrayof string' (as) but pyqt5 translates that into 'array of variant' (av).

我不需要在该列表中放入任何实际数据,只是需要
类型的签名正确即可。

I never need to put any actual data in that list, I just need the typesignature to be correct.

在PyQt5中有什么方法可以做到这一点吗?也许使用QDBusArgument?

Is there any way to do this in PyQt5? Perhaps using QDBusArgument?

推荐答案

我...我开始使用它了。哇。那是一次竞选。

I... I got it working. Wow. That was a campaign.

我不知道到底出了什么问题,我无法从PyQt5来源中确切地了解到发生了转化。但是,我确实研究了 QDbusArgument()。 python中没有与此相关的文档,并且由于主要差异,C ++文档毫无价值,因此我参考了源代码。在 sip / QtDbus / qdbusargument.sip 中,我发现了一个完全未记录的新方法,称为 qdbusargument_add 。这映射到python中的 QDbusArgument()。add()。它用于将具有显式类型id 的参数添加到 QDbusArgument 。它有一个用于 QStringList 的特殊情况!

I don't know what is going wrong exactly, I wasn't able to dig out of the PyQt5 source where exactly the conversion happens. However, I did look into QDbusArgument(). There is no documentation for this in python, and the C++ docs are worthless due to major differences, so I took to the source code. in sip/QtDbus/qdbusargument.sip, I discovered a completely undocumented new method called qdbusargument_add. This maps to QDbusArgument().add() in python. It is used to add arguments with an explicit type id to a QDbusArgument. And it has a special case for QStringList!

从那时起,我就蛮横地我想到了 QDbusArgument()的可能性,最后得到了以下内容:

From then I just bruteforced every possibility I could think of with arguments to QDbusArgument(), and finally got the following:

def PropertiesChanged(self, interface, property, values):
    """Sends PropertiesChanged signal through sessionBus.
    Args:
        interface: interface name
        property: property name
        values: current property value(s)
    """
    emptyStringListArg = QDBusArgument()
    emptyStringListArg.add([""], QMetaType.QStringList)
    self.signal.setArguments([interface, {property: values}, emptyStringListArg])
    QDBusConnection.sessionBus().send(self.signal)

如果可以记录 add()函数会很棒,但是我似乎无法将邮件发送到PyQt5邮件列表。我必须先注册吗?

It'd be great if the add() function could be documented, but I can't seem to send messages to the PyQt5 mailing list. Do I have to register first?

这篇关于PyQt5 dbus:强制信号参数的类型签名为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 12:35