Results 1 to 6 of 6

Thread: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

  1. #1
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    Hi,

    I have an QML Gui in which I need to display a varying number of rectangles (at varying locations in the UI) every x seconds (triggered by a signal from C++ code, no fixed intervall). Rectangles should only be visible until the next batch of rectangles is drawn.

    I.e. I need to have some way to create a bunch of QML objects at runtime, position them and then destroy them again before the next batch is created.
    What is the recommended way to do this?

  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: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    Is the maximum number of rectangles known? If so then the easiest thing would be to precreate them and just show or hide them on demand. If not then you can either create items in C++ using QDeclarativeComponent or pass enough info to QML to create items there using Component and its createObject method.
    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. #3
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    Ok, creating the objects in QML is the way I like best.

    Creating them works fine but now I cannot access them when I want to destroy() them.
    I would need some list to keep track of them but the things I tried are not working.
    (using var rects = new Array() with push() does not work, because I"m not in a Script Element and having a Javascript for each dynamic list I want to keep seems overkill)
    and using property list<Rectangle> rects and append() does not work, because the list always seems to be empty).

    Here a small code example:

    Qt Code:
    1. // init a global list here
    2.  
    3. Connections {
    4. target: ticker
    5. onTick: {
    6. if (t%2 == 0) {
    7. var comp = Qt.createComponent("Rect.qml") //can I use the Rectangle type directly somehow?
    8. var new_rect = comp.createObject(page, {"x":50, "y":50})
    9.  
    10. // alternatively
    11. // var new_rect = Qt.createQmlObject('import QtQuick 1.0; Rectangle {color: "red"; width: 20; height: 20}', page);
    12.  
    13. // add to list
    14. }
    15. else {
    16. // destroy() all rects and clear list here
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #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: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    What do you need all the rectangles for?

    As for the following:
    //can I use the Rectangle type directly somehow?
    Yes, you can:

    javascript Code:
    1. Item {
    2. Component {
    3. id: component
    4. Rectangle {
    5. // put whatever you want here
    6. }
    7. }
    8.  
    9. Something {
    10. onSomeEvent: var obj = component.createObject(parent, { ... })
    11. }
    12. }
    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.


  5. #5
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1

    Default Re: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    I have a stream of images on top of which rectangles are shown to mark certain regions/objects. As new regions/objects show up and others disappear, I need to add/destroy rectangles. Technically there is an upper limit to the number of rectangles but I'd rather have them created dynamically.

    At a later point, the user is supposed to be able to interact with the rectangles (click them mostly) so I cannot draw them into the image but need them to be separate objects on top of it.

  6. #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: Drawing a variable number of rectangles every x seconds (Dynamic Objects)

    Quote Originally Posted by xdn View Post
    I have a stream of images on top of which rectangles are shown to mark certain regions/objects. As new regions/objects show up and others disappear, I need to add/destroy rectangles. Technically there is an upper limit to the number of rectangles but I'd rather have them created dynamically.
    You have to remember creating/destroying is always slower than showing/hiding. It's a trade-off decision you have to make.

    As for keeping all the rectangles -- you can create a property that will hold a list of items.
    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.


Similar Threads

  1. Replies: 4
    Last Post: 18th December 2013, 20:02
  2. Drawing rectangles in QT
    By andreahmed in forum Qt Programming
    Replies: 5
    Last Post: 26th February 2012, 19:08
  3. Replies: 1
    Last Post: 19th April 2011, 11:17
  4. Replies: 0
    Last Post: 14th November 2010, 13:36
  5. Replies: 4
    Last Post: 17th January 2006, 17:46

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.