Search code examples
qtqmlqt-quickqquickitem

Trying to understand usage of Item a bit better?


I'm going through a tutorial on QML and they've started using Item as the base for every component they create, after using Rectangle for the first portion of the tutorial.

The thing is, color doesn't seem to be a valid property on Item. I don't understand why.

Then, I looked at a bunch of example component definitions on-line from a variety of places, and they seem to all use Item as the top-level class and then immediately after, Rectangle. What's the point of wrapping a Rectangle in Item? Why not just use Rectangle as the top-level class?


Solution

  • You can think of Item as of a non-visual Rectangle that is a bit easier and faster for system to paint as, well, there is nothing to paint. It doesn't really make much difference of whether you use Item or Rectangle as a top level element - it is just a good programming practice not to use more resources than you need.

    You will appreciate Item more if you start creating your own non-visual elements at some point.