[solved] Creating own components?
-
wrote on 5 Apr 2012, 12:15 last edited by
Hello,
I want to use buttons in QML.
Therefor, I wrote this code
@ Rectangle {
id: rectangle1
height: 50
color: "#ffffff"
radius: 5
anchors.left: item1.right
anchors.leftMargin: 0
anchors.right: parent.right
anchors.rightMargin: 0
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
transformOrigin: Item.BottomRight
border.width: 2
border.color: "#000000"Text { id: text2 text: qsTr("button's caption") anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter transformOrigin: Item.Center font.bold: true font.pixelSize: 20 } MouseArea { id: mouse_area1 z: 1 hoverEnabled: true anchors.fill: parent anchors.bottom: page.top anchors.top: page.bottom onClicked: { // button's function } } }@
and because I don't want to rewrite this or copy it always when needed, I want to create a component that includes the code above.
!http://i46.img-up.net/Screenshotq3f3.png(Qt Quick, Elements)!Thank you
-
wrote on 5 Apr 2012, 12:31 last edited by
you can write this code in a separate file say Button.qml and it will be automatically available to you just like the available built-in components
-
wrote on 5 Apr 2012, 12:35 last edited by
Oh, and where should I save the file, so that it is available from all my projects?
(my project directory is c:\qtsdk\projects)What should I write in the file in addition ?
-
wrote on 5 Apr 2012, 13:13 last edited by
you ll have to save the file in the folder in which you have the .pro file of ur project...
you ll have to do so for all the projects for which u need the button element... -
wrote on 5 Apr 2012, 13:19 last edited by
Ok, I saved the file and added it to my project.
!http://e96.img-up.net/Screenshota8zh.png(Button.qml)!But I can't load it via the menu with the elements like "Border Image", "Image", "Item", "Rectangle".
-
wrote on 13 Apr 2012, 14:58 last edited by
You should write a qmldir for that .qml file.
"Modules":http://doc.qt.nokia.com/main-snapshot/qdeclarativemodules.html -
wrote on 18 Apr 2012, 17:51 last edited by
To access to component created. You have to be sure that you import dir of this.
for example:
thinking in an order, you could create a folder named myComponents inside: c:\qtsdk\projects
so: c:\qtsdk\projects\myComponents\Button.qml exists.
At first in your main.qml you type: "import myComponents" then you can write Button{} an it will work fine.
Best,
Fernando. -
wrote on 18 Apr 2012, 23:46 last edited by
Good! iam learning QML too, and it's so very helpfull. Sorry for my poor english.