June 10, 2011

open-src open-src
Lab Rat
24 posts

An id problem, please help me.

 

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

  1. Item{
  2.  
  3. .........
  4.     GridViewDelegate{
  5.         id:gridViewDelegate
  6.         width: parent.width/5; height: parent.height/3
  7.     }
  8.  
  9.  
  10.     GridView{
  11.         id:gridView
  12.         anchors.fill: parent
  13.  
  14.         model: pictureModel
  15.         delegate: gridViewDelegate
  16.     }
  17. .........
  18. }

7 replies

June 10, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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

  1.  Component {
  2.            id: gridViewComponent
  3.            GridViewDelegate{        
  4.                   id:gridViewDelegate        
  5.                    width: parent.width/5; height: parent.height/3    
  6.             }
  7.     }
  8. delegate: gridViewComponent

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

June 10, 2011

open-src open-src
Lab Rat
24 posts

  1. Vijay Bhaska Reddy
  2.  
  3. Thanks for your reply!
  4. In fact,my problem is:
  5. 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?
  6.  
  7. Here is PictureGridView.qml:
  8. @import Qt 4.7
  9.  
  10. Item {
  11.    anchors.fill: parent
  12.    property int cellWith: gridView.cellWidth
  13.    property int cellWith: gridView.cellHeight
  14.  
  15.    GridView {
  16.        id:gridView
  17.        flow: GridView.TopToBottom
  18.        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
  19.        focus: true
  20.    }
  21.  
  22. }

Here is PictureGridViewDelegate.qml:

  1. import Qt 4.7
  2.  
  3. Item{
  4.     property alias mode: image.fillMode
  5.  
  6.     Image{
  7.         id:picture
  8.  
  9.         visible: true
  10.         smooth: true
  11.         opacity: 1
  12.         fillMode: Image.PreserveAspectCrop
  13.         source: pathName
  14.  
  15.         MouseArea {
  16.             focus: true
  17.             hoverEnabled : true
  18.             anchors.fill: parent          
  19.         }
  20.     }
  21. }

Here is the main.qml:

  1. Item{
  2.  
  3. .........
  4.     GridViewDelegate{
  5.         id:gridViewDelegate
  6.         width: parent.width/5; height: parent.height/3
  7.     }
  8.  
  9.  
  10.     GridView{
  11.         id:gridView
  12.         anchors.fill: parent
  13.  
  14.         model: pictureModel
  15.         delegate: gridViewDelegate
  16.     }
  17. .........
  18. }

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?

June 10, 2011

Hornsj2 Hornsj2
Lab Rat
80 posts

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.

June 11, 2011

open-src open-src
Lab Rat
24 posts

I am too careless!
I modify the main.qml:

  1. Item{
  2.  
  3. .........
  4. *PictureGridViewDelegate{
  5.         id:pictureGridViewDelegate
  6.         source: pathName
  7.         mode: Image.PreserveAspectCrop
  8.     }*
  9.  
  10.     PictureGridView{
  11.         id:pictureGridView
  12.         anchors.fill: parent
  13.  
  14.  
  15.         model: pictureModel
  16.         delegate: pictureGridViewDelegate
  17.  
  18.     }
  19.  
  20. .........
  21. }

But the QtCreator say that “Unable to assign QObject* to QDeclarativeComponent*” in the PictureGridViewDelegate object above.

June 11, 2011

Hornsj2 Hornsj2
Lab Rat
80 posts

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.

June 11, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

  1. PictureGridView{
  2.         id:pictureGridView
  3.         anchors.fill: parent
  4.  
  5.  
  6.         model: pictureModel
  7.         delegate: pictureGridViewDelegate
  8.  
  9.     }

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

June 11, 2011

open-src open-src
Lab Rat
24 posts

@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…

 
  ‹‹ QML Camera example shows device application menu before opening the camera window      mouse event seems to be unavailable in qml ››

You must log in to post a reply. Not a member yet? Register here!