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. How to separate a program in Model/View/Controller

How to separate a program in Model/View/Controller

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 4 Posters 5.3k Views 1 Watching
  • 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.
  • Y Offline
    Y Offline
    yassine26
    wrote on last edited by
    #1

    I want to separete my program in 3 parts: model, view and controller

    @

    import QtQuick 1.0

    Item {

    width: 800
    height: 480
    
    property string ville: lineEdit.text
    
    XmlListModel {
        id: forecastModel
        source: "http://www.google.com/ig/api?weather="+ville+"&hl=fr"
        query: "/xml_api_reply/weather/forecast_information"
        XmlRole { name: "city"; query: "city/@data/string()" }
    }
    
    Rectangle {
        id: editRect
        x:10; y:10
        width: 120
        color: "black"; radius: 2; border.width: 3; border.color: "gray"; height: 30
        TextInput {
            font.pixelSize: 20
            anchors.centerIn: parent
            id: lineEdit
            text: ville
            smooth: false
            color: "#c8c8c8"
            font.family: "Nokia Large"
            focus: true
            Keys.onReturnPressed: ville = lineEdit.text
        }
    }
    
    ListView {
        x: 145; y: 325; width: 594; height: 48;
        model: forecastModel
        delegate: Text {
            font.family: "Univers LT Std"; color: "#c8c8c8"; width: parent.width; font.pixelSize: 30
            text: city
            anchors.centerIn: parent.centerIn
        }
    }
    

    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      Do you mean you want to have model, view, and delegate coming from different QML files.. can you clarify? Your question is not clear. As I see it, they are already separated.

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yassine26
        wrote on last edited by
        #3

        exactly, I want to separate it in 3 QML files

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          You can put XmlListModel in separate file (let's call it MyModel.qml) and use MyModel from your qml code (if it will be in another directory you should import this path). Please read Qt Assistant to get further information

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            That you can always do that.. have three qml files and have your list view, delegate, and model in those files. But you need to connect them all the above qml file. I mean creating concrete objects of those three items and assigning your model element to Model and same for delegate.

            But I don't see a good reason to separate them? do you have any reason.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              loladiro
              wrote on last edited by
              #6

              "This":http://doc.qt.nokia.com/4.7/qdeclarativemodules.html#qml-modules might help you

              1 Reply Last reply
              0
              • Y Offline
                Y Offline
                yassine26
                wrote on last edited by
                #7

                The purpose of separating them is to have a QML file that contains the data model (XMLlistModel), another that includes the views (TextInput, ListView), and the third to start the program

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  yassine26
                  wrote on last edited by
                  #8

                  the problem is ListView can not find the id of XMLlistModel if I separated the two into two files

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DenisKormalev
                    wrote on last edited by
                    #9

                    You should not use them by id, but create new objects from your qml files.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      thisisbhaskar
                      wrote on last edited by
                      #10

                      Read this "Managing Documents":http://doc.qt.nokia.com/4.7/qdeclarativedocuments.html

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        thisisbhaskar
                        wrote on last edited by
                        #11

                        You don't give up :)
                        First read http://doc.qt.nokia.com/4.7/qdeclarativedocuments.html

                        then to solve your problem....

                        XmlModel.qml
                        @ import QtQuick 1.0
                        XmlListModel {
                        id: forecastModel
                        source: "http://www.google.com/ig/api?weather=&hl=fr"
                        query: "/xml_api_reply/weather/forecast_information"
                        XmlRole { name: "city"; query: "city/@data/string()" }
                        }
                        @

                        TextDeligate.qml
                        @ import QtQuick 1.0

                        Component {
                        Text {
                        id : textItem
                        font.family: "Univers LT Std";
                        color: "#c8c8c8";
                        width: parent.width;
                        font.pixelSize: 30
                        text: city
                        anchors.centerIn: parent.centerIn
                        }
                        }
                        @

                        ListItem.qml
                        @ import QtQuick 1.0
                        ListView {
                        x: 145; y: 325; width: 594; height: 48;
                        }
                        @

                        now in your main.qml

                        @ import QtQuick 1.0

                        Item {
                               XmlModel {
                                        id: modelItem
                               } 
                               ListItem {
                                  id: listItem
                                  model: modelItem
                                  deligate: TextDeligate { }
                         }
                        

                        }@

                        There could be syntax errors, please correct them. Understand what I tried to do. Don't just copy :)

                        1 Reply Last reply
                        0
                        • Y Offline
                          Y Offline
                          yassine26
                          wrote on last edited by
                          #12

                          tank you Vijay Bhaska Reddy i try to do it

                          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