python - 运行 QtRemoteObjects 时得到 "Dynamic metaobject is not assigned"

标签 python python-3.x pyqt pyqt5 qtremoteobjects

我尝试使用QRemoteObjects来共享两个以上的对象,但是 我在运行 client.py 示例时收到“动态元对象未分配”警告,但我不知道发生了什么,我的示例工作正常,任何人都可以给我一些建议吗?

server.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtRemoteObjects import *
from faker import Faker

fake = Faker()

class Name(QObject):
    sig_name = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.name = ''
        self.startTimer(1000)

    def timerEvent(self, event):
        self.name = fake.name()
        self.sig_name.emit(self.name)

class Email(QObject):
    sig_email = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.startTimer(1000)

    def timerEvent(self, event):
        self.sig_email.emit(fake.email())


class Server(QObject):
    def __init__(self):
        super().__init__()
        self.name = Name()
        self.email = Email()

        host = QRemoteObjectHost(QUrl('local:server'), self)
        r1 = host.enableRemoting(self.name, 'name')
        r2 = host.enableRemoting(self.email, 'email')

        print([r1, r2])

    def print_name(self, x):
        print(x)

app = QCoreApplication([])
s = Server()
app.exec()

client.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtRemoteObjects import *

class Client(QObject):
    def __init__(self):
        super().__init__()
        node = QRemoteObjectNode(self)
        node.connectToNode(QUrl("local:server"))

        self.remote_name = node.acquireDynamic('name')
        self.remote_email = node.acquireDynamic('email')

        self.remote_name.initialized.connect(self.onInitName)
        self.remote_email.initialized.connect(self.onInitEmail)

    def onInitName(self):
        self.remote_name.sig_name.connect(self.print_info)

    def onInitEmail(self):
        self.remote_email.sig_email.connect(self.print_info)

    def print_info(self, x):
        print('-->:', x)

app = QCoreApplication([])
c = Client()
app.exec()

在我在终端一运行python server.py并在终端二运行python client.py之后。 我在二号航站楼收到了如下警告。 1

最佳答案

在 C++ 中,您可以使用 2 种方法购买副本:

观察第二种方法时,使用了QRemoteObjectDynamicReplica,它是一个通过复制属性、信号和槽动态创建的类对象,但不包含节点类,因此它不是精确的副本,因此具有 the docs 等缺点指出:

There are generated replicas (replicas having the header files produced by the Replica Compiler), and dynamic replicas, which are generated on-the-fly. This is the class for the dynamic type of replica.

When the connection to the Source object is made, the initialization step passes the current property values (see Replica Initialization). In a DynamicReplica, the property/signal/slot details are also sent, allowing the replica object to be created on-the-fly. This can be conventient in QML or scripting, but has two primary disadvantages. First, the object is in effect "empty" until it is successfully initialized by the Source. Second, in C++, calls must be made using QMetaObject::invokeMethod(), as the moc generated lookup will not be available.

(强调我的)

对于 PyQt,它仅支持第二种方法,因此您会收到指示可能存在问题的警告消息。

关于python - 运行 QtRemoteObjects 时得到 "Dynamic metaobject is not assigned",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59624965/

相关文章:

当参数中添加引号时,Python MySQLdb 查询返回错误

python - 验证 JSON 中的多个节点?

python - 改变 QProgressbar() 的颜色

python-3.x - PyInstaller ModuleNotFoundError --paths 标志似乎不起作用

python - 在unicode字符串中转换字节字符串

python - 定义 True,如果未定义,则导致语法错误

Python - CSV 文件,确保答案与询问的内容在同一行

python - 如何使用 PyQt Signal Slot 连接两个类?

python - 槽在哪个线程中执行,我可以将其重定向到另一个线程吗?

python - 如何获取 QTableWidgetItem QCombobox 的单元格位置