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:
- Item{
- .........
- GridViewDelegate{
- id:gridViewDelegate
- width: parent.width/5; height: parent.height/3
- }
- GridView{
- id:gridView
- anchors.fill: parent
- model: pictureModel
- delegate: gridViewDelegate
- }
- .........
- }
7 replies
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.
- 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?
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.
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.
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.
You must log in to post a reply. Not a member yet? Register here!

