Results 1 to 17 of 17

Thread: Can I create object in QML?

  1. #1
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Can I create object in QML?

    I want my program to create a new rectangle whenever I click on a button. So if I click it five times, it will create five Rectangles. How can I do this in QML?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    You need to have a component and create an object from it every time a click occurs on a mouse area.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    chong_kimkeang (14th November 2012)

  4. #3
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    Now I can create the object but the problem is the objects that I create are standing on the same x,y whenever I zoom the image that is the background. How can the objects go to the right position whenever i zoom the background? Ex:
    I have an image that is the background then I create some rectangles by click a button. When I want this app to be bigger or expend its height or width, how can I make all the objects of rectangle to be zoom in their position? because when I change the side of the background, all the width and height have change but the rectangle's x and y are not change. Can you help me?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    Quote Originally Posted by chong_kimkeang View Post
    How can the objects go to the right position whenever i zoom the background?
    You can move each item you create anywhere you want e.g. by settings its x and y properties.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    it not the thing easy like this. it like you are using StickyNote to stick it on the background and while the background is zooming, all the stickyNote will zoom according to the parent and they are at the correct location. But I can't get the right location of all the stickyNote when zoom.

    In my app, i create the rectangle as the component that i use it as the stickyNote and I call it using function createObject(parent,{x,y}) in onClicked event. of course I can create them but when I zoom the background they are not change their position to the correct location(x,y) but they still remember their old position(x,y) that is not fit with the background that have been zoomed. What can I do that?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    How do you "zoom" the background?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    Press the Maximize button or zoom by the width or height that we have these functionality from the form.
    Last edited by chong_kimkeang; 22nd November 2012 at 04:32.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    So that's not zooming, that's resizing. In this case setting absolute values for x and y will not work because they will still be at the same place when the item is resized. You need to specify the position in fractions of the position of the resized item (e.g. by putting the items you create in a layout or making a property bind between the width/height of the parent item and x/y of the created item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    chong_kimkeang (22nd November 2012)

  11. #9
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    It sound complex, how can I bind the width/ height of the parent item and x/y of the created item if the x/y of the item are not change while I resize the background? could you give me example of code because I have never use binding?

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    Quote Originally Posted by chong_kimkeang View Post
    could you give me example of code because I have never use binding?
    Of course you have, here is a simple binding between two items:

    javascript Code:
    1. Rectangle {
    2. id: root
    3. Rectangle {
    4. x: root.width/3
    5. y: 10+root.height/4
    6. width: 100
    7. height: 100
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    This is not the dynamic re-locate that stickyNote. If I create many stickyNote then when I resize they will go to the same place and all my sticky note can be moved over the background. So how can they catch their new location(x,y) when I resize the background?
    Last edited by chong_kimkeang; 22nd November 2012 at 05:14.

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    Create a binding for the dynamic objects you create. You'll have to lookup in the docs, how to do that as I have never done that before. I know it is possible from within C++, possibly from JavaScript you can do that too. If not then recalculate all the positions yourself after you resize the parent item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following 2 users say thank you to wysota for this useful post:

    chong_kimkeang (22nd November 2012), TheIndependentAquarius (30th July 2014)

  16. #13
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    I have calculate to find the distance between each stickyNote and the background in percentage. I think it can work because after that I can multiply it with the width and height of the background. Unfortunately, when I resize, the percentage is also changed to lower than before that cause the stickyNote location unchangeable. The problem only that the component's location don't change while resizing. How can I change them?

    Is there any property or function for controlling resize?

  17. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    Quote Originally Posted by chong_kimkeang View Post
    The problem only that the component's location don't change while resizing. How can I change them?
    You set it by changing the "x" and "y" properties of the object. Here is a basic example:

    javascript Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. width: 500
    5. height: 500
    6.  
    7. Rectangle {
    8. id: obj
    9. x: 0
    10. y: 0
    11. width: 20
    12. height: 20
    13. color: "red"
    14. }
    15.  
    16. Timer {
    17. property bool reverse: false
    18. interval: 10
    19. running: true
    20. repeat: true
    21. triggeredOnStart: true
    22. onTriggered: { obj.x += reverse ? -1 : 1; obj.y += reverse ? -1 : 1; if(obj.x == 0 || obj.x == parent.width) reverse = !reverse; }
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 


    Just remember that's an imperative approach in a declarative world.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #15
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    When create the object of rectangle or others that is in main.qml, the coordination(x,y) are correctly change according to their parent whenever we zoom or what but my problem is that the object that I create is not in the same file, in ButtonStickyNote.qml file that when I press on a button, the stickyNote is created with the code

    createStickyNote.qml
    Qt Code:
    1. function createObjectStickyNote()
    2. {
    3. var component=Qt.createComponent("ButtonStickyNote.qml")
    4. if(component.status == Component.Ready)
    5. {
    6. component.createObject(imageInside,{"x":10,"y":10}) //imageInside is its parent
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    So when I move the stickyNote, its x,y changed Ex: 100,138 to 200,400 but when I resize it, still it is 200 and 400. I t must change to other x and y to be suitable for the whole background. So how can I change it correctly?

    Capture.jpg

  19. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can I create object in QML?

    How many times do I have to repeat it? Change the "x" and "y" properties of the object you created earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #17
    Join Date
    Sep 2012
    Posts
    66
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I create object in QML?

    Only whenever the user resize the background because when I resize the background, it seems the app refresh. all the item and object in my app have refresh to the correct location but only stickyNote is not at the correct location, it stand on the same location. Like when u resize Ms Power point , the slide in Ms Power point must be smaller or bigger and change is x,y to fit with the whole App. So I just want to know how can I make my stickyNote get the correct x,y when I resize it?
    Last edited by chong_kimkeang; 23rd November 2012 at 02:26.

Similar Threads

  1. Unable to create qwtPlot object
    By jtso8 in forum Qwt
    Replies: 7
    Last Post: 23rd January 2012, 06:43
  2. create a Object by its name.
    By weixj2003ld in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2011, 08:43
  3. How do I create an Object at runtime?
    By Cayan in forum Newbie
    Replies: 2
    Last Post: 28th June 2010, 07:01
  4. finding dynamically create object
    By abrou in forum Newbie
    Replies: 2
    Last Post: 5th March 2008, 21:17
  5. Can't create an object : initialisation problem ?
    By Nyphel in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2007, 09:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.