Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    [solved] Creating own components?

    QML and Qt Quick
    5
    8
    2954
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      pascal95abc 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

      Qt 4.7.3
      Symbian OS v9.4 / Symbian^1, S60 5th Edition
      on N97

      1 Reply Last reply Reply Quote 0
      • M
        mikealfa 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

        1 Reply Last reply Reply Quote 0
        • P
          pascal95abc 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 ?

          Qt 4.7.3
          Symbian OS v9.4 / Symbian^1, S60 5th Edition
          on N97

          1 Reply Last reply Reply Quote 0
          • M
            mikealfa 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...

            1 Reply Last reply Reply Quote 0
            • P
              pascal95abc 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".

              Qt 4.7.3
              Symbian OS v9.4 / Symbian^1, S60 5th Edition
              on N97

              1 Reply Last reply Reply Quote 0
              • J
                jackyang last edited by

                You should write a qmldir for that .qml file.
                "Modules":http://doc.qt.nokia.com/main-snapshot/qdeclarativemodules.html

                1 Reply Last reply Reply Quote 0
                • K
                  kahon 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.

                  1 Reply Last reply Reply Quote 0
                  • D
                    DiegoBR last edited by

                    Good! iam learning QML too, and it's so very helpfull. Sorry for my poor english.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post