使用PyQt5中的QML

时间:2015-09-23 11:50:21

标签: python qt qml qtquick2 pyqt5

我正在尝试使用PyQt5和QML编写程序。

QML部分(MainWindow):

import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Dialogs 1.1
import QtQuick.Layouts 1.0
import QtQuick.Controls.Styles 1.4

ApplicationWindow
{
property int backgroundNumber: 1
objectName : "mainWindow"

title: qsTr("e-Servicing Software")
visible : true
width: 1200
height: 800
minimumHeight: 600

color : 'white'



RowLayout
{
    id: mainLayout
    anchors.fill: parent
    spacing:1
    //Left rectangle
    Rectangle
    {
        objectName : "leftRectangle"

        color:'white'
        Layout.fillHeight : true
        Layout.minimumWidth: 230
        Layout.preferredWidth: 230
        Layout.maximumWidth: 230
        Layout.minimumHeight: 150
        Layout.fillWidth: true

        ColumnLayout
        {
             Layout.fillHeight : true
             Layout.fillWidth: true
             anchors.fill: parent
             spacing : 2

            ColumnLayout
            {
                id: layoutItemLeft
                anchors.fill: parent
                spacing: 0

                Rectangle
                {
                    id: leftPanelHeader
                    color:'#CBE25F'
                    Layout.fillWidth: true
                    Layout.preferredHeight: 50
                    Layout.alignment: Qt.AlignTop
                }

                ButtonImageAndText
                {
                    id: buttonAddPatient
                    Layout.fillWidth: true
                    Layout.preferredHeight: 40
                    anchors.top: leftPanelHeader.bottom

                    textColor: "#CBE25F"
                    backgroundColor : "white"
                    colorOnPress : "yellow"
                    colorOnRelease : "white"
                    fileToLoad: "AjoutPatientPanel.qml"

                    text: "Nouveau Patient"
                    imgSource : "img/add-29-29.png"

                    //marginLeft : 30

                    itemWidth : imageWidth + textWidth
                    textAligmenemt : Qt.AlignLeft
                    imageRightMarge : 20
                }

                ButtonImageAndText
                {
                    id: buttonOrganisation
                    Layout.fillWidth: true
                    Layout.preferredHeight: 40
                    anchors.top: buttonAddPatient.bottom

                    text: "Organisation"
                    imgSource : "img/Organisation29-29.png"

                    textColor: "#CBE25F"
                    backgroundColor : "white"
                    colorOnPress : "yellow"
                    colorOnRelease : "white"

                    fileToLoad: "ListOrganisationPanel.qml"

                    //marginLeft : 30
                    //itemWidth : imageWidth
                    itemWidth : imageWidth + textWidth

                    textAligmenemt : Qt.AlignLeft
                    imageRightMarge : 10
                }

                Separator
                {
                    Layout.fillWidth: true
                    Layout.preferredHeight: 1.01
                    Layout.bottomMargin: 7
                }
            }

            ColumnLayout
            {
                id: layoutListViewLeft
                spacing: 2
                Layout.fillWidth: true

                PatientListView
                {
                    id : listViewPatient
                    objectName: "listViewPatient"

                    Layout.fillWidth: true

                    Layout.fillHeight : true
                    anchors.fill: parent
                    clip: true
                    anchors.bottomMargin: logoEove.height
                }

                Image
                {
                    id : logoEove
                    objectName: "imageListViewPatient"

                    clip:true
                    Layout.fillWidth:true
                    source : "img/Logo200.png"
                    Layout.alignment: Qt.AlignBottom
                }
            }
        }
    }


    //Middle rectangle
    Rectangle
    {
        id : rectangleMiddle
        objectName: "rectangleMiddle"

        color:'white'
        Layout.fillHeight : true
        Layout.fillWidth: true

        Layout.minimumWidth: 350
        Layout.preferredWidth: 350
        Layout.maximumWidth: 350
        Layout.minimumHeight: 150

        //Used To handle screen to display
        Loader
        {
            id: middlePanelLoader
            objectName : "middlePanelLoader"
            anchors.fill: parent
            Layout.fillWidth: true

            source: "ShowPatientPanel.qml"
        }
    }


    //Right rectangle
    Rectangle
    {
        id: rectangleRight
        objectName: "rectangleRight"

        color:'white'
        Layout.fillHeight : true
        Layout.fillWidth: true
        Layout.minimumHeight: 150

        Layout.minimumWidth: 800
        Layout.preferredWidth: 800

        Loader
        {
            id: rightPanelLoader
            objectName: "rightPanelLoader"

            anchors.fill: parent
            Layout.fillWidth: true
            source: "RightPanelContent.qml"
        }


        /*RightPanelContent
        {
            Layout.fillHeight : true
            Layout.fillWidth: true
            anchors.fill: parent
        }*/
    }
}
}

Python代码:

import sys
from PyQt5.QtCore import QObject, QUrl, pyqtSlot, QVariant
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from App.AppMain import MainApp

if __name__ == "__main__":
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()

    ctx = engine.rootContext()
    engine.load('QML/Projet_Loader.qml')
    win = engine.rootObjects()[0]

py_mainapp = MainApp(ctx, win)
ctx.setContextProperty("AppMain", py_mainapp)
win.show()
sys.exit(app.exec())

文件2

import sys
from PyQt5.QtCore import QObject, pyqtSlot, QVariant

class MainApp(QObject):
    def __init__(self, context, parent=None):
        super(MainApp, self).__init__(parent)

        self.win = parent
        self.ctx = context

        toto = self.win.findChild(QObject, "imageListViewPatient").property("source")
        #print(str(toto))
        self.win.findChild(QObject, "imageListViewPatient").setProperty("source", "txt")
        toto = self.win.findChild(QObject, "imageListViewPatient").property("source")
        #print(str(toto))

        rect = self.win.findChild(QObject, "rectangleRight")
        rect2 = rect.findChild(QObject, "rectangleRightToDraw").setProperty("color", "blue")


        self.win.findChild(QObject, "buttonUpdatePatient").clicked.connect(self.test3)
        #print("a " + str(a))
        #c = self.win.findChildren(QObject)



        #for item in c :
        #    print(str(item))
        #    b = item.findChild(QObject, "rectangleMiddlePatient")
        #    print(str(b))


def test3(self):
    print("okss")

我有几个问题, 第一个是我应该如何构建python和QML交互。

第二个是我使用Loader在点击事件上切换某个面板。但是如果我将panel1切换到panel2并再次切换panel2和panel1,我的点击事件就不再与我的PyQtSignal连接了。我想我是以错误的方式使用装载机,我应该从python开车,但我不知道怎么做。我该怎么办 ?

我知道我做错了什么,找不到好的资源来帮助我。 如果你知道一些好的教程或源代码,我会接受它!

0 个答案:

没有答案