首页 > 解决方案 > 我如何在pyqt5中绑定qtquick按钮控件

问题描述

您好,我如何在 pyqt5 中绑定一个 qtquick 按钮,我有两个文件,一个用于 qml,一个用于 python,现在我希望该按钮应该做一些事情,例如打印一些东西,这样我现在可以如何绑定,

Button.py

import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine





if __name__ == "__main__":
  app = QApplication(sys.argv)
  engine = QQmlApplicationEngine()
  ctx = engine.rootContext()
  ctx.setContextProperty("main", engine)

  engine.load('Button.qml')

  win = engine.rootObjects()[0]
  win.show()
  sys.exit(app.exec_())


Button.qml
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Dialogs 1.1

ApplicationWindow {
 title: qsTr("Test Invoke")

 width: 200
 height: 100

 Button{
  y : 70
  text : "About"
  onClicked: {
   print('Hello')
  }

 }
}

标签: buttonbindingpyqt5qtquickcontrols

解决方案


这是一个使用 QML QtQuick(信号和槽)的小例子。程序文本中的一些解释。试试看:

按钮.py

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

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

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

    @pyqtSlot(str)
    def text1(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("Button.qml")
    engine.quit.connect(app.quit)
    sys.exit(app.exec_())

按钮.qml

import QtQuick 2.7
//import QtQuick.Window 2.1
import QtQuick.Controls 1.4
//import QtQuick.Dialogs 1.1
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    title:   qsTr("Test Invoke")
    width:   400
    height:  100
    color:   "whitesmoke"

    GridLayout {
        anchors.top:     parent.top
        anchors.left:    parent.left
        anchors.right:   parent.right
        anchors.margins: 9

        columns: 4
        rows: 2
        rowSpacing: 10
        columnSpacing: 10

        Text {
            text: qsTr("Enter text")
        }

        // Text input box
        TextField {
            id: firstString
        }

        Button {
            height: 40
            Layout.fillWidth: true
            text: qsTr("Send text for processing")

            Layout.columnSpan: 2

            onClicked: {
                // call the slot to process the text
                main.text1(firstString.text)
            }
        }

        Text {
            text: qsTr("Result")
        }

        // Here we see the result of text processing
        Text {
            id: textResult
        }

    }

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

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

在此处输入图像描述


推荐阅读