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. An id problem, please help me.

An id problem, please help me.

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 3.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.
  • O Offline
    O Offline
    open-src
    wrote on last edited by
    #1

    Someone can tell me that why the id, gridViewDelegate, can not be referenced in GridView object in the snippet of my main.qml below:
    @Item{

    .........
    GridViewDelegate{
    id:gridViewDelegate
    width: parent.width/5; height: parent.height/3
    }

    GridView{
        id:gridView
        anchors.fill: parent
    
        model: pictureModel
        delegate: gridViewDelegate
    }
    

    .........
    }@

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

      deligate takes a component, not a concrete item. GridView is going to create instances of your component when ever needed based on the data in Model.

      One thing you can do is to place your GridViewDeligate inside a component item and assing component item's id to deligate

      @ Component {
      id: gridViewComponent
      GridViewDelegate{
      id:gridViewDelegate
      width: parent.width/5; height: parent.height/3
      }
      }
      delegate: gridViewComponent @

      This should work :) and I hope you understand why we need to do this.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        open-src
        wrote on last edited by
        #3

        @Vijay Bhaska Reddy

        Thanks for your reply!
        In fact,my problem is:
        I have three qml documents, PictureGridView.qml、PictureGridViewDelegate.qml、 and main.qml. The model has exposed to qml from C++, its name is pictureModel. I create the GridViewDelegate object, Why I can't reference it using its id?

        Here is PictureGridView.qml:
        @import Qt 4.7

        Item {
        anchors.fill: parent
        property int cellWith: gridView.cellWidth
        property int cellWith: gridView.cellHeight

        GridView {
            id:gridView
            flow: GridView.TopToBottom
            highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
            focus: true
        }
        

        }@

        Here is PictureGridViewDelegate.qml:
        @import Qt 4.7

        Item{
        property alias mode: image.fillMode

        Image{
            id:picture
        
            visible: true
            smooth: true
            opacity: 1
            fillMode: Image.PreserveAspectCrop
            source: pathName
        
            MouseArea {
                focus: true
                hoverEnabled : true
                anchors.fill: parent          
            }
        }
        

        }
        @

        Here is the main.qml:

        @Item{

        .........
        GridViewDelegate{
        id:gridViewDelegate
        width: parent.width/5; height: parent.height/3
        }

        GridView{
            id:gridView
            anchors.fill: parent
        
            model: pictureModel
            delegate: gridViewDelegate
        }
        

        .........
        }@

        According to qml documents API, the GridViewDelegate is a qml compomnent type, why it have to be placed into a _ Component_ element?

        Btw: I am puzzle about multifile programming in qml, can you give me any guidance again?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hornsj2
          wrote on last edited by
          #4

          First off, you are never declaring a PictureGridView element or a PictureGridViewDelegate element in your main.qml file.

          There is no type named GridViewDelegate in QML, custom type names are based off the file name (so the type name would be PictureGridViewDelegate).

          The types you have available based on those files are

          1. PictureGridView
          2. PictureGridViewDelegate

          Try using those types in place of GridViewDelegate and GridView.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            open-src
            wrote on last edited by
            #5

            I am too careless!
            I modify the main.qml:
            @
            Item{

            .........
            PictureGridViewDelegate{
            id:pictureGridViewDelegate
            source: pathName
            mode: Image.PreserveAspectCrop
            }

            PictureGridView{
                id:pictureGridView
                anchors.fill: parent
            
            
                model: pictureModel
                delegate: pictureGridViewDelegate
            
            }
            

            .........
            }@
            But the QtCreator say that "Unable to assign QObject* to QDeclarativeComponent*" in the PictureGridViewDelegate object above.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hornsj2
              wrote on last edited by
              #6

              I'm not sure becasue I haven't bothered to look at the docs. However, can a root object in QML be of type Item?

              Try rectangle.

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

                mm... you never have model and deligate available in your PictureGridView. If you look at your PictureGridView qml file, you have an item and then you have an GridView inside that item.

                @PictureGridView{
                id:pictureGridView
                anchors.fill: parent

                    model: pictureModel
                    delegate: pictureGridViewDelegate
                
                }@
                

                I would suggest you to go through few introductory videos / tutorial of qml.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  open-src
                  wrote on last edited by
                  #8

                  @Vijay Bhaska Reddy

                  Thanks a lot.
                  I confused the concept of the component, I have believed the top-level item element MUST be Item\Component\Rectangle\FocusScope\QtObject in a single qml once, then I think it a mistake now...

                  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