Showing posts with label QtQuick. Show all posts
Showing posts with label QtQuick. Show all posts

2013-03-07

QML Resizable rectangle from scratch


It has been forever since I've posted anything on my blog.  Apologies to my follower...  Meanwhile, I have learned quite a lot about QML and QtQuick over the past 6 months or so.  I will have to go dig through my code and find some snippets that I think will be helpful to those joining the Qt universe.

This is a (relatively) simple Item I created which allows the user to click any one of the four walls and drag it to resize the rectangle.  You can also left click on the rectangle and drag it around.  This example does NOT include any limitations to dragging, so if you can pretty much destroy the rectangle by dragging one wall right over the opposite wall.  However, I thought it best to post something simple rather than something more complicated and hard to read for newcomers.  Relationships between anchors and QML objects can get quite confusing fast, and you have to avoid anchor loops.  This is nothing more than an example of multiple anchors working together which actually works, but is not finished product.  


Maybe someone will get some use out of it.


Cheers!
-----------------------------------------------------------------------------

//import QtQuick 2.0
import QtQuick 1.1

 Item {
    id: resizeRectItem
    property real rectWidth
    property real rectHeight

    rectWidth: rightWall.x - leftWall.x
    rectHeight: bottomWall.y - topWall.y
    //opacity: 1

    Rectangle {
        id: resizeRect
        width: rectWidth
        height: rectHeight
        color: "blue"
        anchors.right: rightWall.left
        anchors.bottom: bottomWall.top
        anchors.top: topWall.bottom
        anchors.left: leftWall.right
    }
    MouseArea {
        id: resizeRectDragMA
        width: rectWidth -5
        height: rectHeight -5
        anchors.centerIn: parent
        drag.target: resizeRectItem
        drag.axis: Drag.XandYAxis

        onPressed: {
            resizeRect.color = "green"
            resizeRectItem.anchors.centerIn = null
        }
        onReleased: {
            resizeRect.color = "blue"
        }
    }

    Rectangle {
        id: topWall
        x: -100
        y: -100
        width: rectWidth
        height: 4
        color: "red"
    }
    MouseArea {
        id: topWallMA
        anchors.fill: topWall
        drag.target: topWall
        drag.axis: Drag.YAxis
        onPressed: {
            leftWall.anchors.top = topWall.bottom
            rightWall.anchors.top = topWall.bottom
        }
    }
    Rectangle {
        id: bottomWall
        x:-100
        y: 100
        width: rectWidth
        height: 4
        color: "purple"
    }
    MouseArea {
        id: bottomWallMA
        anchors.fill: bottomWall
        drag.target: bottomWall
        drag.axis: Drag.YAxis
        onPressed: {
            leftWall.anchors.bottom = bottomWall.top
            rightWall.anchors.bottom = bottomWall.top
        }
    }
    Rectangle {
        id: leftWall
        x: -100
        y: -100
        width: 4
        height: rectHeight
        color: "yellow"
    }
    MouseArea {
        id: leftWallMA
        anchors.fill: leftWall
        drag.target: leftWall
        drag.axis: Drag.XAxis
        onPressed: {
            topWall.anchors.left = leftWall.right
            bottomWall.anchors.left = leftWall.right
        }
    }
    Rectangle {
        id: rightWall
        x: 100
        y: -100
        width: 4
        height: rectHeight
        color: "orange"
    }
    MouseArea {
        id: rightWallMA
        anchors.fill: rightWall
        drag.target: rightWall
        drag.axis: Drag.XAxis
        onPressed: {
            topWall.anchors.right = rightWall.left
            bottomWall.anchors.right = rightWall.left
        }
    }
    states: [
        State {
             name: "hide"
             PropertyChanges {
                 target: resizeRectItem
                 opacity: 0.0
             }
        }
    ]
}

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-05-11

Good QML QtQuick C++ Tutorial (finally)

I searched long and hard for something like this, so I thought I'd pass it along to those who are Googling for the same thing.  There are tons of tutorials and samples and examples out there that use the Qt API with C++, but I found it quite difficult to find any complete and comprehensive tutorials and/or code that includes the method that integrates QML as the User Interface and C++ for the application logic.  Here's what I was looking for:

http://kgronholm.blogspot.com/2010/10/qt-quick-game-programming.html

or here

http://quitcoding.com/?page=work#qtquick

Thanks to Kaitsu and QUIt Coding for the good work and generosity.

Some notes on installation:

I had to install mesa-libGLU-devel in my Fedora 16 system in order to get the game to run.

I had another problem getting the game to run.  Although the project would build, when I tried to run it I would get an error saying "No such file or directory" referring to the "content" folder where all of the QML is kept.  The build was not copying this folder into the shadow build copy that it was then attempting to run from.

So I sent an email to QUItcoding.com.  Kaitsu answered me back very quickly and let me know that I only needed to disable "shadow build" under the build settings in the "projects" view ( found on the left side of QtCreator).  That fixed the issue immediately.

I should also mention that I replaced the line:
import Qt 4.7

at the top of all the .qml files with:
import QtQuick 1.0

I don't think this really mattered since the newer versions retain previous version functionality, but I thought I'd mention it anyway.