Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Switch that controls other qml files



  • Using PySide2, Qt Quick Controls, and Py Creator:

    In my Project/qml/pages/settingsPage.qml, I created a switch:

            Rectangle {
                id: rectangle1
                width: 220
                height: 60
                visible: true
                color: "#2c303b"
                radius: 10
                border.width: 0
                Layout.fillHeight: false
                Layout.alignment: Qt.AlignLeft | Qt.AlignTop
                Layout.fillWidth: true
                anchors.leftMargin: 20
                anchors.topMargin: 20
    
    
    
                Switch {
                    id: mySwitch
                    anchors.left: parent.left
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    display: AbstractButton.IconOnly
                    spacing: 20
                    font.wordSpacing: 0
                    rightPadding: 0
                    padding: 0
                    font.pointSize: 15
    
                    onToggled: {
                    //backend.
    
                    }
    
                    Label {
                        id: labelTextName1
                        x: 120
                        width: 200
                        color: "#e8e9ec"
                        text: qsTr("Switch")
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        anchors.rightMargin: -170
                        font.pointSize: 14
    
                    }
                }
            }
    

    Inside the onToggled for the switch, I want to create a property in my project/qml/pages/homePage.qml and change the color of the background in homePage.qml. What function do I need to create inside the main.py.

    homePage.qml:

    Item {
    id: item1
    Rectangle {
    id: rectangle
    color: "#2c313c"
    //want to change this color
    anchors.fill: parent
    ...
    }
    }


  • @final_software said in Switch that controls other qml files:

    homePage.qml

    Where is your instance defined? You have to instantiate an object before you use it or manipulate it.



  • I made little changes to my code so that:

    In the settingsPage.qml file, I have this inside of onToggled:
    backend.switchValue = toggled

    In the homePage.qml file, I have this inside one of the rectangles I've created:

    visible: backend.switchValue()

    Lastly, in main.py file:
    customTitleBar = Signal(bool)
    @Slot()
    def switchValue(self):
    return customTitleBar.isToggled

    What am I doing wrong?


Log in to reply