如何在pyqt5中绑定qtquick button控件

如何在pyqt5中绑定qtquick button控件,button,binding,pyqt5,qtquickcontrols,Button,Binding,Pyqt5,Qtquickcontrols,您好,我如何在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 _

您好,我如何在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')
  }

 }
}

下面是一个使用QML QtQuick信号和插槽的小示例。 节目文本中的一些解释。 试试看:

Button.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_())
Button.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
        }
    }    
}
复制者