Results 1 to 12 of 12

Thread: QML Qt.createQmlObject

  1. #1
    Join Date
    Apr 2012
    Location
    Vienna, Austria
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QML Qt.createQmlObject

    Hello everyone,

    after some years with QWidgets, i decided to join the "QML stream" and give QML a try.

    Yet, i've run into problems i can't even really explain (is there any good Error-Handling for QML??).

    Problem 1: adding a ToolButton to a ToolBar
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Controls 1.3
    3. import QtQuick.Layouts 1.1
    4.  
    5. ApplicationWindow {
    6. id: applicationWindow
    7. width: 800
    8. height: 600
    9. title: "test"
    10.  
    11.  
    12. toolBar: ToolBar {
    13. id: mainWindowToolbar
    14. RowLayout {
    15. id: mainWindowToolbarLayout
    16. anchors.fill: parent // problem 2
    17. }
    18. }
    19.  
    20. Component.onCompleted: {
    21. Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}',
    22. mainWindowToolbarLayout,
    23. "TestToolButton");
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    If i (re)start the application 3 times, it crashes 2/3 times and also throws an error: "Qt.createQmlObject(): Component is not ready".
    Google told me that this one of the most unrelieable error message in QML.
    Which brings us to Problem #3: in the documentation of Qt.createQmlObject it states that an QtScript error is thrown, so i encapsulated the Qt.createQmlObject part into an try and catch block - yet i have no idea how to retrieve the errors from that qmlErrors() function.

    Problem 2: mainWindowToolbarLayout + "anchors.fill: parent" prints the following things into the output-console:
    QML Item: Binding loop detected for property "layoutHeight"
    Qt Code:
    1. "qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitWidth""
    2. qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitWidth"
    3. "qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitWidth""
    4. qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitWidth"
    5. "qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitHeight""
    6. qrc:/qml/MainWindowUI:16:14: QML ToolBar: Binding loop detected for property "implicitHeight"
    To copy to clipboard, switch view to plain text mode 
    What? The documentation isn't doing anything different?

    What brings us to my final question:
    is there any sane way to debug such things 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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    Hello everyone,

    after some years with QWidgets, i decided to join the "QML stream" and give QML a try.

    Yet, i've run into problems i can't even really explain (is there any good Error-Handling for QML??).

    Problem 1: adding a ToolButton to a ToolBar
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Controls 1.3
    3. import QtQuick.Layouts 1.1
    4.  
    5. ApplicationWindow {
    6. id: applicationWindow
    7. width: 800
    8. height: 600
    9. title: "test"
    10.  
    11.  
    12. toolBar: ToolBar {
    13. id: mainWindowToolbar
    14. RowLayout {
    15. id: mainWindowToolbarLayout
    16. anchors.fill: parent // problem 2
    17. }
    18. }
    19.  
    20. Component.onCompleted: {
    21. Qt.createQmlObject('import QtQuick 2.0; Rectangle {color: "red"; width: 20; height: 20}',
    22. mainWindowToolbarLayout,
    23. "TestToolButton");
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    Why would you use createQmlObject for such thing?

    If i (re)start the application 3 times, it crashes 2/3 times and also throws an error: "Qt.createQmlObject(): Component is not ready".
    Did you try splitting your component definition into multiple lines to avoid the semicolon after the import statement?

    What? The documentation isn't doing anything different?
    Hard to say without knowing what documentation you mean.

    is there any sane way to debug such things in QML?
    Yes. Use the QML debugger and analyzer that comes with Qt Creator. However your errors are very simple so there is no need to use a debugger to avoid them.
    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
    Apr 2012
    Location
    Vienna, Austria
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Qt.createQmlObject

    Quote Originally Posted by wysota View Post
    Why would you use createQmlObject for such thing?
    Because the documentation states that this is only one of two options.

    src: https://qt-project.org/doc/qt-5-snap...tcreation.html

    So Qt.createComponent looks like some overkill when i just want to add some ToolButton.


    Quote Originally Posted by wysota View Post
    Did you try splitting your component definition into multiple lines to avoid the semicolon after the import statement?
    Why should i? The documentation does the same in it's examples.
    https://qt-project.org/doc/qt-5-snap...-string-of-qml

    Quote Originally Posted by wysota View Post
    Hard to say without knowing what documentation you mean.
    http://doc.qt.io/qt-5/index.html
    https://qt-project.org/doc/qt-5-snap...s-toolbar.html

    Quote Originally Posted by wysota View Post
    Yes. Use the QML debugger and analyzer that comes with Qt Creator. However your errors are very simple so there is no need to use a debugger to avoid them.
    Nice to hear that they are simple, yet i found no reference to what they mean.

  4. #4
    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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    So Qt.createComponent looks like some overkill when i just want to add some ToolButton.
    I think what wysota meant is: "why don't you just add a ToolButton"?

    Qt Code:
    1. toolBar: ToolBar {
    2. RowLayout {
    3. anchors.fill: parent
    4.  
    5. ToolButton {
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Apr 2012
    Location
    Vienna, Austria
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Qt.createQmlObject

    Quote Originally Posted by anda_skoa View Post
    I think what wysota meant is: "why don't you just add a ToolButton"?
    As the title may suggest, i want/need to do this at runtime :/

  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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    So Qt.createComponent looks like some overkill when i just want to add some Tool Button.
    I think createQmlObject is a larger overkill but anyway I didn't mean to use createComponent. You can declare a component inline in the original QML document and just instantiate it when needed. See the docs on Component element.

    By the way, the title doesn't suggest you want to do anything dynamically.
    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
    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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    As the title may suggest, i want/need to do this at runtime :/
    Maybe it would help to know what exactly you are trying to achieve.

    Cheers,
    _

  8. #8
    Join Date
    Apr 2012
    Location
    Vienna, Austria
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Qt.createQmlObject

    @QML ToolBar: Binding loop detected for property "implicitWidth""
    Something was anchored by me when it shouldn't have been (as some items are automatically anchored). Maybe it was the toolbar - i can't remember right now, but that one sounds right.

    This is how i'm creating things at runtime right now:
    Qt Code:
    1. function createByData(targetQmlSourceCode, parentItem, targetQmlModuleName) {
    2. try {
    3.  
    4. var component = Qt.createQmlObject(targetQmlSourceCode,
    5. parentItem,
    6. targetQmlModuleName);
    7. return component;
    8. }
    9. catch (error) {
    10. console.error("There were QML errors:" + JSON.stringify( error.qmlErrors));
    11. }
    12.  
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    The documentation was a little but fuzzy about details like how to access the "qmlErrors" object and that you have to use try{} and catch(){} blocks for the cases where Qt.createQmlObject would otherwise suicide-bomb the qmlapplicationengine.
    Now that my frustration with this problem has faded, i'd like to apologize if i might have sounded a bit harsh.
    Thank you guys for your contribution!

  9. #9
    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: QML Qt.createQmlObject

    It would still be interesting why you need that at all.

    Such imperative code is commonly unnecessary, most often there is a way cleaner solution.

    Cheers,
    _

  10. #10
    Join Date
    Apr 2012
    Location
    Vienna, Austria
    Posts
    17
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Qt.createQmlObject

    Quote Originally Posted by anda_skoa View Post
    It would still be interesting why you need that at all.

    Such imperative code is commonly unnecessary, most often there is a way cleaner solution.

    Cheers,
    _
    I wrote a third-party plugin handler for my application and needed to create the "shortcut-icons" for those plugins at runtime.
    Generating a ListModel and throw that into a ListView would also have done the trick i guess, but i prefered actual Buttons to keep up with the design of the application.

  11. #11
    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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    I wrote a third-party plugin handler for my application and needed to create the "shortcut-icons" for those plugins at runtime.
    Generating a ListModel and throw that into a ListView would also have done the trick i guess, but i prefered actual Buttons to keep up with the design of the application.
    You could have declared the list of buttons in the plugin and then pass that list to your handler so that it can put them in the toolbar.

    Your current design is a secutiry risk -- you are accepting code from a third-party plugin that you execute on behalf of your object.
    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.


  12. #12
    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: QML Qt.createQmlObject

    Quote Originally Posted by axed View Post
    I wrote a third-party plugin handler for my application and needed to create the "shortcut-icons" for those plugins at runtime.
    Generating a ListModel and throw that into a ListView would also have done the trick i guess, but i prefered actual Buttons to keep up with the design of the application.
    Why would having the data in a model keep you from using buttons?

    Creating instances of any type based on a model is a standard use case, implemented with the Repeater element.

    Cheers,
    _

Tags for this Thread

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.