- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何从 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 docs ,WorkerScript
无法使用 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/
使用 QML WorkerScript 时,请求(通过 postMessage() 发送)是否已排队(并在单个处理程序线程上执行),或者是否存在可能性/危险两个连续的 postMessage() 将在
我有一个 BusyIndicator,它应该在进行大量计算时旋转并在计算完成时停止。 我认为 WorkerScript 是正确的方法,但是 from here , .js 文件中的辅助(计算线程)
我有一个缓慢的 I/O 操作,我需要从 QML UI 进行控制。 I/O 接口(interface)在 C++ 中。基本上,当用户按下按钮时,我需要发送消息并从设备获得响应。我希望用户在等待响应时能够
如何从 Qt Quick WorkerScript 调用 Python 方法? 序言: 我已成功通过注册 QObject 的子类从 QML 应用程序访问我的 Python 方法: class Foo(
我是一名优秀的程序员,十分优秀!