Results 1 to 11 of 11

Thread: QDeclarativeView question

  1. #1
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDeclarativeView question

    Hi all, I'm using a QDeclarativeView object to open a qml file. I use it as such:

    MainView w;
    w.setSource(QUrl::fromLocalFile(settings.value("ma in_view").toString()));

    Where MainView inherits from QDeclarativeView

    In my mainview.qml file I use a Loader to load various QML files depending on what the user selects from menus. How can I tell in C++ when the underlying QML structure changes? Is there a signal I can hook into?

    Thanks

  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: QDeclarativeView question

    What do you need that functionality for?
    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
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDeclarativeView question

    wysota, I want to cache some QDeclarativeItem * in a Hash.

    I find this look up tends to be slow:
    QDeclarativeItem *obj = this->rootObject()->findChild<QDeclarativeItem*>(item);


    I thought I'd empty the cache each time a new qml file was loaded by my loader.

    I found a work around, I delete from the cache items that have not been looked up after 10 minutes.

  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: QDeclarativeView question

    Quote Originally Posted by cmessineo View Post
    wysota, I want to cache some QDeclarativeItem * in a Hash.
    That's a solution, I'm asking about the problem.

    I find this look up tends to be slow:
    QDeclarativeItem *obj = this->rootObject()->findChild<QDeclarativeItem*>(item);
    You should (almost) never need that in real world app.
    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
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDeclarativeView question

    I'm sure the guys at Digia would not have created methods like findChild or setProperty if they didn't expect people to use them in real world.

    Right now I use this code, so I can update qml properties on a touch screen display module. Data comes in over a serial port and depending upon the data I update a property in qml.

    QDeclarativeItem *obj = this->rootObject()->findChild<QDeclarativeItem*>(item);

    if(!obj) {
    qDebug() << "[QML] no item with objectName: " << item;
    bool found = obj->setProperty(property.toLatin1(),value);
    }

  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: QDeclarativeView question

    Quote Originally Posted by cmessineo View Post
    I'm sure the guys at Digia would not have created methods like findChild or setProperty if they didn't expect people to use them in real world.
    findChild was created long before Digia took over Qt Anyway, the fact that a method exists doesn't automatically mean it should be used for things you are trying to use it for. My point is that in a general case you shouldn't "extract" items from a Qt Quick scene into C++.

    Right now I use this code, so I can update qml properties on a touch screen display module. Data comes in over a serial port and depending upon the data I update a property in qml.
    You should rather expose your data model to Qt Quick and connect your items to the data model there.
    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.


  7. #7
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDeclarativeView question

    Well that is the problem and it is my fault for not communicating properly, there is no data model. The QML is not known by me, I don't know what other people will create, so I have to find the child item and update a property if I can. I just didn't want to use findChild("root") if I had made that call 100ms ago and it was successful. I can cache things the way I want, I just can't tell if the qml has changed, so I can kill the cache. Most people use a Loader for navigation of their QML applications. I was hoping to find a way to figure out when the Loader loaded up some new qml file. The only thing I have control over is the C++ qmlviewer app that I created. I don't know what the qml will be.

  8. #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: QDeclarativeView question

    Quote Originally Posted by cmessineo View Post
    Well that is the problem and it is my fault for not communicating properly, there is no data model.
    There is always some data model. At first glance I can see you have some objects that have some properties that have some values. Now it's just a matter of wrapping them in a nice API.

    The QML is not known by me, I don't know what other people will create, so I have to find the child item and update a property if I can.
    In my opinion you should rather do the opposite -- inform QML that some value has changed and let the author of the document handle that.

    I just didn't want to use findChild("root") if I had made that call 100ms ago and it was successful.
    How do you know that object still exists 100ms later?

    I can cache things the way I want, I just can't tell if the qml has changed, so I can kill the cache.
    "QML" doesn't have to "change" for objects to stop existing. Consider the following code:

    javascript Code:
    1. ... {
    2. id: root
    3. property Item someItem: null
    4.  
    5. Component {
    6. id: c
    7. Item { objectName: "someObject" }
    8. }
    9.  
    10. Component.onCompleted: root.someItem = c.createObject(root)
    11.  
    12. Timer {
    13. interval: 10000
    14. running: true
    15. onTriggered: if(root.someItem) { root.someItem.destroy(); root.someItem = null }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    How do you know whether "someObject" exists or not, even if it existed just a moment ago?

    I was hoping to find a way to figure out when the Loader loaded up some new qml file.
    That's pretty easy as the loader will emit an itemChanged signal when it does that however it will not make your code work. Especially that you don't know if there is any Loader at all.
    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.


  9. #9
    Join Date
    Jun 2012
    Posts
    18
    Thanked 9 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDeclarativeView question

    I thought there would be a mechanism for me to hook into when something like this happened:

    Timer {
    interval: 10000
    running: true
    onTriggered: if(root.someItem) { root.someItem.destroy(); root.someItem = null }
    }

    Or even if a QML Item was created through javascript. Some signal that an object was created or destroyed.

  10. #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: QDeclarativeView question

    Quote Originally Posted by cmessineo View Post
    I thought there would be a mechanism for me to hook into when something like this happened:

    Timer {
    interval: 10000
    running: true
    onTriggered: if(root.someItem) { root.someItem.destroy(); root.someItem = null }
    }
    There are too many situations like that that can cause you trouble. You will not patch and stitch each and every one.

    Or even if a QML Item was created through javascript. Some signal that an object was created or destroyed.
    You can do that using QGraphcisItem API but that will be hell slow since you'd be monitoring every item in the scene. I assure you this is a wrong way to approach your problem. The proper way is to expose some API to QML scripts and ask authors of the scritps to use that API.

    E.g. something along the lines of:

    javascript Code:
    1. Item {
    2. id: myObject
    3. ChangeListener {
    4. name: "myObject"
    5. properties: [ "width", "height", "x", "y" ] // listen to changes on these properties
    6. onChanged: myObject[propName] = value // propName and value are parameters of onChanged signal
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    or even:
    javascript Code:
    1. Item {
    2. id: myObject
    3. objectName: "myObject"
    4. ChangeListener {
    5. target: myObject
    6. properties: [ "width", "height", "x", "y" ]
    7. // updating the property is done in C++
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 2nd July 2015 at 07:18.
    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.


  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDeclarativeView question

    Quote Originally Posted by cmessineo View Post
    The QML is not known by me, I don't know what other people will create, so I have to find the child item and update a property if I can
    So how do you know that the item exists?

    I agree with wysota, this sounds like a very broken setup.

    The cleanest and incidentally at the same time easiest way is to export an object with the data property as a context property and let the QML side use it wherever it wants to.

    Cheers,
    _

Similar Threads

  1. How to unload a QDeclarativeView object
    By nestuser in forum Newbie
    Replies: 0
    Last Post: 17th August 2012, 06:45
  2. How QDeclarativeView destruction
    By xman_ss in forum Qt Programming
    Replies: 3
    Last Post: 27th April 2012, 07:55
  3. Drag into a QDeclarativeView
    By frankiefrank in forum Qt Quick
    Replies: 1
    Last Post: 3rd October 2011, 12:12
  4. Resize QDeclarativeView
    By Globulus in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2011, 17:31
  5. Using QDeclarativeView::Show()
    By proj_symbian in forum Qt Quick
    Replies: 17
    Last Post: 30th May 2011, 19:26

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.