Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Replace current view with other qml view file
QtWS25 Last Chance

Replace current view with other qml view file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 5 Posters 684 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bwylegly
    wrote on last edited by bwylegly
    #1

    Hi.

    Is it possible to replace "current" qml view that is displayed in window with other one, by specifying its path?

    I.e. When my application starts, first qml it loads is LoginView.qml.
    What I want to do, is to load MasterView.qml file (containing NavigationBar component) if user logs in successfully.
    Reason for that is NavigationBar should be present only if logged in.

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import QtQuick.Window 2.14
    import assets 1.0
    import CMS 1.0
    
    Window {
    
        id: loginView
        objectName: "loginView"
    
        visible: true
        width: 640
        height: 480
        title: qsTr("Clinic Management System")
    
        signal loginClicked(string textField_usr, string textField_pwd);
    
        Connections {
            target: loginController
            onLoginSuccessful : {
                console.log("Success")
                //LOAD MASTERVIEW.QML
    
            }
            onLoginFailed: {
                console.log("Failed") 
                //POPOUT 
            }
              
        }
    
        onClicked: {
            loginController.loginButtonClicked(username_textField.text, password_textInput.text)
        }
    }
    
    
    
    1 Reply Last reply
    0
    • Quang PhuQ Offline
      Quang PhuQ Offline
      Quang Phu
      wrote on last edited by Quang Phu
      #2

      @bwylegly ,
      With me, I use StactView for change many views. You can try it, refer document here:
      https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jay1
        wrote on last edited by
        #3

        You can also try to use a SwipeView or StackView with "Loader" that loads the page when required and can reduce memory foot print. Sample example is as below, you can try to add required conditions to change the page view.

        Hope this helps

        SwipeView {
            id: view
        
            currentIndex: 1
            anchors.fill: parent
        
            Item {
                id: firstPage
        		Rectangle {
        			width: 200; height: 200
        
        			Loader {
        				id: loader
        				focus: true
        			}
        
        			MouseArea {
        				anchors.fill: parent
        				onClicked: {
        					loader.source = "secondPage.qml"
        				}
        			}
        		}
            }
            Item {
                id: secondPage
        		Rectangle {
        			width: 200; height: 200
        
        			Loader {
        				id: loader
        				focus: true
        			}
        
        			MouseArea {
        				anchors.fill: parent
        				onClicked: {
        					loader.source = "firstpage.qml"
        				}
        			}
        		}
            }
        }
        
        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZakiAsadi
          wrote on last edited by ZakiAsadi
          #4

          Also you could use Qt.createComponent:

                     //LOAD MASTERVIEW.QML
                      var component = Qt.createComponent("MasterView.qml");
                      var win = component.createObject(loginView);
                      win.show();
          
          1 Reply Last reply
          0
          • D Offline
            D Offline
            dogbear
            wrote on last edited by
            #5

            @bwylegly
            There are many ways to do what you want. This one be too simplistic for your needs but in my experience, simplicity is beauty.
            I often create two or more of my screens as separate QML files (of type Item) and create an instance of all of them in the main window, with only the start-up one showing.

            Window {
                MasterView {
                    id: masterView
                    anchors.fill: parent
                    visible: false
                }
                LogonScreen {
                    id: logonScreen
                    anchors.fill: parent
                }
            }
            

            I also like to play around with different styles, like having the logon screen be a dialog but have an opaque background so you can see the workspace behind but are unable to interact with them until you log on.

            This way does require the QML files to be loaded first and not dynamically as your question might allude to.

            Hope this helps.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved