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. [Solved] ListElements (ListModel) defined in other qml files?

[Solved] ListElements (ListModel) defined in other qml files?

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 7.5k 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.
  • T Offline
    T Offline
    Thanana
    wrote on 4 Oct 2011, 13:34 last edited by
    #1

    I got a fonctionnal ListModel defined like this :

    @
    ListModel {
    id: leftGrid
    ListElement { icon: "Images/1.png" }
    ListElement { icon: "Images/2.png" }
    ListElement { icon: "Images/3.png" }
    ListElement { icon: "Images/4.png" }
    }
    @

    The thing is that I'd like to define ListElement in separate qml files but I really don't know what to type in these other qml files, and how to call them from the main file...

    Then, I'd like to add some fields to SOME Elements but it won't work with the PathView
    definition :

    @
    Component {
    id: componentDelegate
    Item {
    Image {
    source: icon
    }
    }
    }
    @

    right ?

    Don't know how to deal with this problem too... :(

    Any help with be welcomed, thanks in advance.

    [EDIT: code formatting, please wrap in @-tags, Volker]

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on 4 Oct 2011, 14:05 last edited by
      #2

      Hello,

      you can write your model in separate file:

      Data.qml
      @

      import QtQuick 1.0

      ListModel {
      id: leftGrid
      ListElement { icon: "Images/1.png" }
      ListElement { icon: "Images/2.png" }
      ListElement { icon: "Images/3.png" }
      ListElement { icon: "Images/4.png" }
      }
      @

      And than you can use it in other QML file:

      main.qml

      @
      import QtQuick 1.0

      PathView{
      model: Data {}
      }
      @

      I can't understand your second question :( Can you explain a bit more?

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • T Offline
        T Offline
        Thanana
        wrote on 4 Oct 2011, 14:25 last edited by
        #3

        Hello, and thx for your answer.

        About the first question, what i want to place in qml files are the ListElements, NOT the ListModel. Something like :
        @ListModel {
        id: leftGrid
        ListElement { myWidget1 }
        ListElement { myWidget2 }
        ListElement { myWidget3 }
        ListElement { myWidget4 }
        }@

        or maybe something like :
        @ListModel {
        id: leftGrid
        myWidget1
        myWidget2
        myWidget3
        myWidget4
        }@
        I don't know.. but I want ListElements in qml files

        About the 2nd question, let's consider 2 ListElements (and independantly from question 1). So we got this :
        @ListElement { nb: "3" }
        ListElement { nb: 3
        icon: "Images/2.png" }@

        And then I'd like to do something like :
        @Component {
        id: componentDelegate
        Item {
        Image {
        if icon.isDefined() {
        source: icon
        }
        else {
        source : "Images/DefaultImage.png"
        }
        }
        }
        }@

        Or maybe something like :

        @Image {
            source: icon ? icon : "Images/DefaultImage.png"
        }@
        

        Do you know what I mean ?

        Thx in advance for your help, i'm really lost on it...

        1 Reply Last reply
        0
        • T Offline
          T Offline
          task_struct
          wrote on 4 Oct 2011, 14:41 last edited by
          #4

          On second question. AFAIK all ListElements should have equal number of roles so you can write

          @
          ListElement {
          nb: "3"
          icon: ""
          }
          ListElement {
          nb: 3
          icon: "Images/2.png"
          }
          @

          and than check

          @
          Component {
          id: componentDelegate
          Item {
          Image {
          if ( icon != "" ) {
          source: icon
          }
          else {
          source : "Images/DefaultImage.png"
          }
          }
          }
          }
          @

          For first question. What is type of myWidget1? Is it inherits Item?

          I think it is possible to write something like this:
          @
          ListModel {
          id: leftGrid
          ListElement { roleName: myWidget1 }
          ListElement { roleName: myWidget2 }
          ListElement { roleName: myWidget3 }
          ListElement { roleName: myWidget4 }
          }
          @

          and in your delegate:

          @
          Component {
          id: componentDelegate
          Item {
          id: roleName
          }
          }
          @

          but I`m not sure if this works.

          "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

          • Linu...
          1 Reply Last reply
          0
          • T Offline
            T Offline
            Thanana
            wrote on 4 Oct 2011, 15:25 last edited by
            #5

            @Component {
            id: componentDelegate
            Item {
            Image {
            if ( icon != "" ) {
            source: icon
            }
            else {
            source : "Images/DefaultImage.png"
            }
            }
            }
            }@

            gives me the error "Unexpected symbol : if"

            and about the first question, I don't know what the write in myWidget1 :/ That's just the general structure I want to have !

            Some ideas about how to use "if" and about the other question ?

            Thx in advance :)
            Gui

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on 4 Oct 2011, 15:37 last edited by
              #6

              In regard to using "if", try:
              @
              Component {
              id: componentDelegate
              Item {
              Image {
              source: (icon !="")?icon:"Images/DefaultImage.png"
              }
              }
              }
              @
              or
              @
              Component {
              id: componentDelegate
              Item {
              Image {
              source: {
              if (icon !="") {
              return icon
              } else {
              return "Images/DefaultImage.png"
              }
              }
              }
              }
              }
              @

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Thanana
                wrote on 4 Oct 2011, 15:58 last edited by
                #7

                Ok, it works for the condition statement :)

                So, here is a summary for my main problem :

                I got a fonctionnal ListModel defined like this :

                @ListModel {
                id: leftGrid
                ListElement { icon: "Images/1.png" }
                ListElement { icon: "Images/2.png" }
                }@

                The thing is that I'd like to define ListElement in separate qml files but I really don't know how to do it...

                I wrote the qml like this :

                @//myWidget.qml
                import QtQuick 1.0

                ListElement {
                icon: "Images/X.png"
                }@

                But I don't know how to "invoke" or "instanciate" it in my main file...

                I tried :

                @ListModel {
                id: leftGrid
                ListElement { icon: "Images/1.png" }
                myWidget //qml file
                }@

                and :

                @ListModel {
                id: leftGrid
                ListElement { icon: "Images/1.png" }
                ListElement { myWidget }
                }@

                Both doesn't work...

                Any help with be welcomed, thanks in advance.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  task_struct
                  wrote on 4 Oct 2011, 19:57 last edited by
                  #8

                  It seems that it is not possible to have ListElement in separate file. ListElement normally contains a few lines of code. So I don`t see a problem to have ListModel in separate file. If you are trying to create a model that contains some widgets, may be "Reapeater":http://doc.qt.nokia.com/4.7-snapshot/qml-repeater.html will be more convenient.

                  "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

                  • Linu...
                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Thanana
                    wrote on 5 Oct 2011, 09:35 last edited by
                    #9

                    Ok will try it :)
                    Thx !

                    1 Reply Last reply
                    0

                    5/9

                    4 Oct 2011, 15:25

                    • Login

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