2015-09-23 39 views
-1

我在QML中更改視圖/文件時遇到問題。在qrc文件中,我有main.qml和second.qml。在main.cpp中我的代碼開始我的應用程序:從Qt(main.cpp)QML中查看

QQuickView view; 
view.setSource(QUrl(("qrc:///main.qml"))); 
view.show(); 

在main.qml是按鈕,應該改變以second.qml,但我不知道用什麼方式做到這一點。我讀過關於qml的內容,但是在任何地方我找到了這些示例

的main.qml:

Item { 
id: screen; width: 320; height: 480; 

signal exitApp() 
signal qmlSignal(string addressIP, int portTCP) 

Rectangle { 
    id: background 
    anchors.fill: parent; color: "#ffffff"; 
    anchors.rightMargin: 0 
    anchors.bottomMargin: 0 
    anchors.leftMargin: 0 
    anchors.topMargin: 0 

    Button { 
     id: loginBtn 
     text: qsTr("RUN") 
     anchors.right: parent.right 
     anchors.rightMargin: 100 
     anchors.left: parent.left 
     anchors.leftMargin: 100 
     anchors.bottom: parent.bottom 
     anchors.bottomMargin: 170 
     anchors.top: tcpRow.bottom 
     anchors.topMargin: 10 
     onClicked: qmlSignal(addressIPTextField.text, parseInt(portTCPTextField.text)) 
    } 
} 
    Row { 
     id: tcpRow 
     x: 8 
     width: 309 
     height: 100 
     anchors.top: ipRow.bottom 
     anchors.topMargin: 10 
     anchors.horizontalCenter: parent.horizontalCenter 

     Label { 
      id: portTCPLabel 
      height: 20 
      text: qsTr("Port TCP") 
      anchors.left: parent.left 
      anchors.leftMargin: 10 
      anchors.right: portTCPTextField.left 
      anchors.rightMargin: 10 
      anchors.verticalCenter: parent.verticalCenter 
     } 
}} 

回答

1

您可以使用多個StackView 「屏幕」 之間進行導航。要使現有代碼適用於StackView,將每個屏幕移動到其自己的QML文件可能會更容易。例如,移動background項目進入LoginScreen.qml

Rectangle { 
    id: background 

    // ... 

    Button { 
     onClicked: { 
      qmlSignal(addressIPTextField.text, parseInt(portTCPTextField.text)); 
      StackView.view.push("qrc:/second.qml"); 
     } 
    } 
} 

在這裏,我們使用的StackView附加view屬性來獲取訪問視圖,然後按下第二個屏幕到它。

然後,在main.qml

Window { 
    width: // ... 
    height: // ... 

    StackView { 
     initialItem: LoginScreen {} 
    } 
} 
+0

感謝您的回覆,但什麼意思'StackView.view'?它是從main.cpp中查看的? – Szymson

+0

正如我在我的回答中提到的那樣,它是'StackView'的附加屬性。請閱讀我鏈接到的文檔。 – Mitch