PyQt使用QT远程对象



我有一个使用QT远程对象用c++编写的服务器和客户端:https://doc.qt.io/qt-5.11/qtremoteobjects-index.html

我想在python中创建一个测试服务器,供c++客户端连接。有没有任何方法可以使用python中的.rep文件来创建和托管qt远程对象(cpp客户端可以连接到(?

以下是一个示例客户端:main.cpp

#include <QCoreApplication>
#include <rep_myserver_replica.h>
#include <QSharedPointer>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRemoteObjectNode repNode;
QSharedPointer<MyRemoteObjectReplica> mo;
if (!repNode.connectToNode(QUrl("tcp://127.0.0.1:6112")))
{
return 0;
}
mo.reset(repNode.acquire<MyRemoteObjectReplica>());
auto f = [&]()
{
auto f2 = [](QString s) { std::cout << "Answer arrived: " << s.toStdString() << std::endl; };
if (!QObject::connect(mo.get(),&MyRemoteObjectReplica::magicConvertResponse,f2))
{
std::cerr << "Unable to connect to magicCOnvertResponse()" << std::endl;
}
mo->magicConvert("ALMA");
};
if (!mo->isInitialized())
{
if (!QObject::connect(mo.get(),&MyRemoteObjectReplica::initialized,f ))
{
std::cerr << "Unable to connect to initialized()" << std::endl;
}
}
else
{
f();
}
return a.exec();
}

myserver.rep:

class MyRemoteObject
{
SLOT(magicConvert(QString text))
SIGNAL(magicConvertResponse(QString text))
};

目前(2021年4月(我正在尝试实现相同的功能,Python Server/Source和C++Client/Replica。我没有使用任何rep,最终可以将QObject的孩子注册为来源:

# MyClass.py
from PyQt5.QtCore import QObject, pyqtSlot
class MyClass(QObject):
def __init__(self):
super().__init__()
@pyqtSlot(int, name="someMethod")
def someMethod(self, value):
print("someMethod called with value " + str(value))
# main.py
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtRemoteObjects import QRemoteObjectHost
from MyClass import MyClass
if __name__ == '__main__':
app = QApplication(sys.argv)
api = MyClass()
remoteHost = QRemoteObjectHost(QUrl("local:class"))
remoteHost.enableRemoting(api, "MyClass")
return app.exec()

相关内容

  • 没有找到相关文章