[Qt-qml] hasChildren attribute for Qml Model?

Martin Jones martin.jones at nokia.com
Wed Apr 21 08:14:42 CEST 2010


I've added a hasModelChildren property to the delegate and a couple of helper 
functions to VisualDataModel:

- VisualDataModel::modelIndex(int) returns a QModelIndex which can be assigned 
to VisualDataModel::rootIndex
- VisualDataModel::parentModelIndex() returns a QModelIndex which can be 
assigned to VisualDataModel::rootIndex

which simplifies the previously mentioned example to:

// main.cpp
 int main(int argc, char ** argv)
 {
     QApplication app(argc, argv);
     QDeclarativeView view;

     QDirModel model;
     view.rootContext()->setContextProperty("myModel", &model);

     view.setSource(QUrl("qrc:view.qml"));
     view.show();

     return app.exec();
 }

 #include "main.moc"

// view.qml
 import Qt 4.7

 ListView {
     id: view
     width: 200
     height: 200
     model: VisualDataModel {
         model: myModel
         delegate: Component {
             Rectangle {
                 height: 25; width: 200
                 Text { text: filePath }
                 MouseArea {
                     anchors.fill: parent;
                     onClicked: if (hasModelChildren) view.model.rootIndex = 
view.model.modelIndex(index)
                 }
             }
         }
     }
 }


Martin.

On Tue, 20 Apr 2010 08:34:34 pm ext Stephen Kelly wrote:
> Martin Jones wrote:
> > On Tue, 20 Apr 2010 07:56:17 pm ext Stephen Kelly wrote:
> >> Martin Jones wrote:
> >> > On Mon, 19 Apr 2010 10:09:17 pm ext Stephen Kelly wrote:
> >> >> In QTreeView, that's a [+] which you can click to expand.
> >> > 
> >> > You could add a Q_INVOKABLE method to your model to get the child
> >> > count, similar to the setRoot() method in this example:
> >> > 
> >> > http://qt.nokia.com/doc/4.7-snapshot/qml-
> 
> visualdatamodel.html#rootIndex-
> 
> >> prop
> >> 
> >> 
> >> 
> >> Indeed, however, it gets complicated when an intermediate proxy model
> >> filters out all the children.
> >> 
> >> That is common in my case because it is something like a filesystem
> >> where on one side is the folder structure and on the other side is the
> >> files in the selected folder. In the view that is showing only the
> >> folder structure, hasChildren is true only if it has subfolders. Files
> >> are already filtered out.
> >> 
> >> First I put a method similar to what you describe in the proxy actually
> >> sent into the qml environment, but then when I added another proxy I had
> >> to move the logic to that class. That's not a good solution.
> >> 
> >> Currently I have an invokable method in a globally accessible
> >> application object to figure out if the correct proxy hasChildren at a
> >> particular row. The method will have to be updated if ever the
> >> application changes. It's a bit too much magic for me, so the best
> >> place for a solution is further down the stack in Qt.
> > 
> > We'll add something along the lines of a "hasChildren" property to the
> > delegate.
> 
> Awesome, thanks.
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml

-- 
Martin



More information about the Qt-qml mailing list