2012-07-13

QTreeView in QtQuick/QML Implementation

Okay, so in my last post I posted a duplicate of my question over on the Qt Dev Forums.  Thankfully someone answered quickly and I was able to implement a solution.  Mind you, it still has all sorts of kinks and functions that need to be fixed or realized, but it's a start.  Considering how much time I spent trying to get this to work, I figured that this is one of those things that I really want to share with the community, so before I delve in deep, I thought I'd stop for a second and post the code to my test application.

All this app does is show a QTreeView using QML.  Essentially, it is a boiled down plugin for QML.  The QTreeView class is derived from QWidget.  The QDeclarativeView where my QML "lives" only accepts elements that are derived from QGraphicsObject.  To solve this problem I have wrapped the QTreeView in a QGraphicsProxyWidget.

It sounds complicated, but it really isn't.  Without ever having seen it done however, it was quite difficult to discover,  especially for the nooborn like me.  Answers are much simpler to find once you know what questions to ask.  IMHO it is important to always remember this fact lest we start to labor under the misconception that we are actually intelligent beings or that we ourselves are awesome because we know so muich.  What follows are the terrible diseases that go by many names including "noobphobia", "lorditoverallsia", "assholiosis", and "I-have-forgotten-plain-english-but-I-think-that-using-technical-terms-makes-me-look-smart-so-no-one-knows-what-I-am-saying-blindness".  Just take a look around the forums and through the comments all over the web and you will see that these diseases are spreading like that virus that causes people to turn into flesh eating zombies in the movies.  I think I'd prefer the zombies....

Anyway, here is the code.  Enjoy.  If you have any questions, I'll try to answer, but I'm a noob too, so no promises peeps!

I am using Qt Creator 5.0 Based on Qt 4.8.1 (64 bit) according to Help/About Qt Creator...


This quick test app was created by going to File/New File or Project.../Applications/Qt Quick Application (Built-in Elements). Named the project QTreeTest.  I chose only "Desktop" as my target.


The files I am going to share are:

QTreeTest.pro
testtron.h
treeview.h
testtron.cpp
main.cpp
main.qml
TreeView.qml

I tried to post the code directly to Blogger and it got all wonky of course, so here is a link to a Google Docs folder instead.  Enjoy!

LINK TO ALL THE FILES


NOTE: In order to assign the "treeView" a "hide" state I had to nest it within another rectangle and assign the hide state to the id of that rectangle. The TreeView will reject direct state assignments.
I sure hope this helps somebody. I'm going to come back and clean this up a bit on Monday. Right now, it is almost 6:00 PM and it is FRIDAY! Woo hoo! I am outta here! Cheers!

2012-07-12

QTreeView in QML question (posted to Qt Developer Network too)

UPDATE:  See my later post here for the answer(s) to this question.  Or at least a start.

Hello everybody.  Help!  I'm trying to incorporate a Model/View into an application using QTreeView, QFileSystemModel and qmlRegisterType to create the QTreeView plugin.

If my main.qml looks like this:

import QtQuick 1.1
import TreeView_Plugin 1.0

    TreeView {
        id: myTreeView
        allColumnsShowFocus: true

    }

I get a QTreeView displayed looking like this:



However, when my main.qml looks like this:

import QtQuick 1.1
import TreeView_Plugin 1.0

Rectangle {
    id:myRect
    width: 400
    height: 400

    TreeView {
        id: myTreeView
        allColumnsShowFocus: true
    }
}

All I get is this...


I am not able to insert the "TreeView" into the rest of my QML and I can't figure out why.
If I try to add a property change to x, y, width or height, for example:

    TreeView {
        id: myTreeView
        x: 15
        y: 15
        allColumnsShowFocus: true
    }


I get error:  Invalid property assignment: "x" is a read-only property

Of course I will post plenty more code if someone thinks they might be able to help.

The application already uses a QDeclarativeView with an OpenGL backend like so:

QGLWidget *glWidget = new QGLWidget(this); 
setViewport(glWidget); 
 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);


There is of course a bit more code involving setting the FileSystemModel, etc, but I don't believe it is relevant.  I am really quite baffled here.  Obviously, I need the QTreeView to show within the rest of my QML or there really is no point.

If anyone could point me to a way to do this, I would very much appreciate it.  I'm pretty new to Qt and I readily admit that I sometimes have no idea what is going on "under the hood", especially when it comes to the whole MOC file paradigm.

Whatever way I can get this done is fine with me.  I really thought the plugin (qmlRegisterType) method was the way to go, because I'm using it to create an OpenGL window for animations in a separate part of the app, but, as you can see, it ain't working.

Thanks for your help!
------------------------------------
UPDATE  2012-07-13:
I received an answer in the Qt Dev Forums.  Here's a link.
This looks promising, because I was already thinking I would probably have to wrap the class/object in another object in order to get QDeclarativeView to accept it.

What I did not see until after I posted this yesterday, is that QtCreator is producing a warning when I run the first example above (the one WITH the QTreeView displayed).  I don't have it in front of me but it was something along the lines of QDeclarativeView only supports objects derived from QGraphicsObject.  QTreeWidget is NOT a QGraphicsObject, it is derived from QWidget.

I'll let you know what I get working.

I know it's a bit odd posting a question like this, but now you can see the sort of process I go through on a daily basis in the world of programming.  The eternally nooborn, learning never ends.  (did I just coin a word?)