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

Make a conditional rule between combobox value and button.



  • Hi, my friends.
    In my application, I want when the user selects the A or B from combobox and click on the NEXT button the application open different page (Page1.qml for A and Page2.qml for B).

    The below code doesn't work, and I don't have any idea.As I'm not a qt expert, any help would be a great help.

    Thank you.

    
    import QtQuick 2.6
    import QtQuick.Controls 2.2
    
    ScrollablePage {
        id: page
    
        Column {
            id: column
            spacing: 40
            width: parent.width
    
            Label {
                width: parent.width
                wrapMode: Label.Wrap
                horizontalAlignment: Qt.AlignHCenter
                text: "Select A or B"
            }
    
            ComboBox {
                model: ["B", "A"]
                anchors.horizontalCenter: parent.horizontalCenter
            }
    
    
            Button {
    
                id: button
                text: "NEXT"
                y:-300
    
                anchors.bottom: parent.bottom
                width: Math.max(button.implicitWidth, Math.min(button.implicitWidth * 2, pane.availableWidth / 3))
                onClicked: {
                    if(qsTr(ComboBox.currentText)==="A"){
                        stackView.push("qrc:/pages/Page1.qml")
                    }
                    else
                        stackView.push("qrc:/pages/Page2.qml")
                }
    
            }
        }
    }
    
    


  • You are using the object wrong because you don't have an id for it.

    set an id for combobox

    ComboBox {
                id: selectionCombo
                model: ["B", "A"]
                anchors.horizontalCenter: parent.horizontalCenter
            }
    

    and use the id to check the condition

    onClicked: {
                    if(qsTr(selectionCombo.currentText)==="A"){
                        stackView.push("qrc:/pages/Page1.qml")
                    }
                    else
                        stackView.push("qrc:/pages/Page2.qml")
                }
    

    Note: I didn't compile and check the functionality. Hope this will solve your issue.



  • @Mammamia thank you my friend. it works.


Log in to reply