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:


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!


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!


  1. This comment has been removed by the author.

  2. Please note: a treeview.cpp file is not necessary in this tiny test app.

  3. I am a beginner in qt and
    I could not find treeview.cpp in the google drive link you've shared and i am getting an error : :-1: error: LNK1181: cannot open input file 'release\main.obj'

    1. It has been so long since I posted this, that I'm not sure what to tell you. However, please notice that in an early comment, I pointed out that a "treeview.cpp file is not necessary in this tiny test app". Also, I believe that up to date versions of QtQuick have classes that fit most situations better than QTreeView. QTreeView always had it's limitations for me.

    2. Thanks for replying. but I figured out how to proceed with it, in qt 5.5 the treeview is availabale in qml implementation. Thanks once again for your time

  4. No problem. I'll bet that solution was way more friendly than my old hacked together version anyway!


Let me know what you think!