在python中使用PyQt5按下按钮时,在QML中更改标签

时间:2018-05-01 11:46:48

标签: python qt pyqt qml pyqt5

我试图用PyQt5和QML做一个简单的任务:有一个按钮可以在点击时更改标签。我按下按钮后能够执行python功能,但我不知道如何更改标签中的文字。

这是一个最小的例子:

main.py

from PyQt5.QtWidgets import *
from PyQt5.QtQml import *
from PyQt5.QtCore import *

import sys

def onClicked():
    print('handler called')

if __name__ == '__main__':
    app = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    engine.load(QUrl('main.qml'))

    win = engine.rootObjects()[0]
    button = win.findChild(QObject, 'myButton')
    button.messageRequired.connect(onClicked)

    sys.exit(app.exec_())

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.3
import QtQuick.Window 2.3

ApplicationWindow{
    title: qsTr('Quomodo')
    id: mainWindow
    width:  480
    height: 640
    visible: true

    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 8
        padding: 8

        Button {
            signal messageRequired
            objectName: "myButton"
            text: qsTr("Work")
            highlighted: true
            onClicked: messageRequired()
        }

        Label {
            text: qsTr("Time")
            anchors.horizontalCenter: parent.horizontalCenter
        }

    } 
}

如何将标签文本更改为" Next",例如?

注意:这不是QML not taking ownership of object received from PyQt slot的重复。这个问题是关于Python和QML之间数据的所有权,并没有回答这个问题。

1 个答案:

答案 0 :(得分:1)

试一试:

from PyQt5.QtGui  import QGuiApplication
from PyQt5.QtQml  import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QUrl


class Main(QObject):
    def __init__(self):
        QObject.__init__(self)

    # signal sending string
    # necessarily give the name of the argument through arguments=['textLabel']
    # otherwise it will not be possible to pick it up in QML
    textResult = pyqtSignal(str, arguments=['textLabel'])

    @pyqtSlot(str)
    def textLabel(self, arg1):
        # do something with the text and emit a signal
        arg1 = arg1.upper()
        self.textResult.emit(arg1)


if __name__ == "__main__":
    import sys
    app    = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    main   = Main()
    engine.rootContext().setContextProperty("main", main)
    engine.load(QUrl('main.qml'))
    engine.quit.connect(app.quit)
    sys.exit(app.exec_())

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.3
import QtQuick.Window 2.3

ApplicationWindow{
    title: qsTr('Quomodo')
    id: mainWindow
    width:  480
    height: 640
    visible: true

    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 8
        padding: 8

        Button {
            objectName: "myButton"
            text: qsTr("Work")
            highlighted: true
            onClicked: {
                // call the slot to process the text
                main.textLabel("Next")
            }
        }

        Label {
            id: textResult
            text: qsTr("Time")
            anchors.horizontalCenter: parent.horizontalCenter
        }
    } 

    // Here we take the result of text processing
    Connections {
        target: main

        // Signal Handler 
        onTextResult: {
            // textLabel - was given through arguments=['textLabel']
            textResult.text = textLabel
        }
    }      
}

enter image description here