gpt4 book ai didi

python - 从 Qt Quick WorkerScript 调用 Python 方法

转载 作者:行者123 更新时间:2023-12-01 02:20:00 26 4
gpt4 key购买 nike

如何从 Qt Quick WorkerScript 调用 Python 方法?

序言:

我已成功通过注册 QObject 的子类从 QML 应用程序访问我的 Python 方法:

class Foo(QtCore.QObject):
@QtCore.pyqtSlot()
def bar(self):
pass # do stuff here

app = QtGui.QGuiApplication([])
QtQml.qmlRegisterType(Foo, 'Foo', 1, 0, 'Foo')
# more boilerplate Qt/Qt Quick

因此,在我的 QML 应用程序中,我可以成功调用 Foo.bar()

据我了解,我应该从 WorkerScript 调用任何长时间运行的函数。

问题:

如何使用 WorkerThread 在后台线程中运行 Foo.bar()

Per the docsWorkerScript 无法使用 import 语法,因此我不确定如何在不导入的情况下访问 Foo

更新:

我需要 UI 能够显示 Foo.bar() 的进度,因为该函数需要一些时间并执行多项操作。

最佳答案

WorkerScript 发送信息的唯一方法已通过sendMessage() :

sendMessage(jsobject message)

Sends the given message to a worker script handler in another thread. The other worker script handler can receive this message through the onMessage() handler.

The message object may only contain values of the following types:

  • boolean, number, string
  • JavaScript objects and arrays
  • ListModel objects (any other type of QObject* is not allowed)

All objects and arrays are copied to the message. With the exception of ListModel objects, any modifications by the other thread to an object passed in message will not be reflected in the original object.

但是由于它读取的所有元素都被复制(ListModel 类型的元素除外),因此不可能使用任何继承自 QObject 类或其方法的对象。

<小时/>

您可以将进度设为pyqtProperty,以便将其公开给QML,并使用槽通过QRunnable从另一个线程更新其值code> 和 QThreadPool,更新是通过 QMetaObject::invokeMethod()

完成的
class Runnable(QRunnable):
def __init__(self, obj):
QRunnable.__init__(self)
# main thread
self.obj = obj

def run(self):
# another thread
self.obj.bar()


class Foo(QObject):
def __init__(self, *args, **kwags):
QObject.__init__(self, *args, **kwags)
self._progress = 0

@pyqtSlot()
def run_bar(self):
self.runnable = Runnable(self)
QThreadPool.globalInstance().start(self.runnable)

progressChanged = pyqtSignal(int)

@pyqtProperty(int, notify=progressChanged)
def progress(self):
return self._progress


@pyqtSlot(int)
def updateProgress(self, value):
if self._progress == value:
return
self._progress = value
self.progressChanged.emit(self._progress)

def bar(self):
for i in range(100):
QMetaObject.invokeMethod(self, "updateProgress",
Qt.QueuedConnection,
Q_ARG(int, i))
QThread.msleep(1000)

然后就可以通过run_bar()来启动它,并通过progress属性来显示:

import QtQuick.Controls 1.4
import QtQuick.Layouts 1.0
import QtQuick 2.0
import Foo 1.0

ApplicationWindow{
width: 300
height: 300
visible: true

Text{
id:txt
text: "press me to start"
anchors.fill: parent
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}

Foo{
id: foo
onProgressChanged: txt.text= progress
}

MouseArea {
anchors.fill: parent
onClicked: foo.run_bar()
}

statusBar: StatusBar {
RowLayout {
anchors.fill: parent
ProgressBar {
value: foo.progress
maximumValue: 100
anchors.fill: parent
}
}
}
}

完整的示例可以在下面的 link 中找到。

关于python - 从 Qt Quick WorkerScript 调用 Python 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com