Results 1 to 2 of 2

Thread: loading QML file as a component from C++ to QQmlApplicationEngine

  1. #1
    Join Date
    Aug 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default loading QML file as a component from C++ to QQmlApplicationEngine

    Hello everyone,

    I am trying to load a qml file to my QQmlApplicationEngine. I guess, since the component is visual item(rect), somehow, engine does not render it.
    Can someone please point me where am i missing?

    Thank you.

    Here is my sample code,


    Qt Code:
    1. /* MyItem.qml */
    2. import QtQuick 2.5
    3.  
    4. Rectangle {
    5. id: top
    6. property string str: "#FF0000"
    7. color: str
    8. width: 10
    9. height: 20
    10. Component.onCompleted: console.log("myitem ctor", width, height, x, y, visible, color);
    11. }
    12.  
    13. /* main.qml */
    14. import QtQuick 2.5
    15. import QtQuick.Window 2.2
    16.  
    17. Window {
    18. visible: true
    19. id: root
    20.  
    21. MouseArea {
    22. id: mouse
    23. anchors.fill: parent
    24. onClicked: {
    25. Qt.quit();
    26. }
    27. }
    28.  
    29. Text {
    30. id: text
    31. text: qsTr("Hello World")
    32. anchors.centerIn: parent
    33. }
    34. }
    35.  
    36. /* main.cpp */
    37. #include <QGuiApplication>
    38. #include <QQmlApplicationEngine>
    39. #include <QQmlComponent>
    40. #include <QQmlEngine>
    41.  
    42. int main(int argc, char *argv[])
    43. {
    44. QGuiApplication app(argc, argv);
    45.  
    46. QQmlApplicationEngine engine;
    47. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    48.  
    49. QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/MyItem.qml")));
    50.  
    51. QObject *childItem = component.create();
    52. QList<QObject *> rootObjects = engine.rootObjects();
    53. QObject *parentItem = rootObjects.first();
    54.  
    55. childItem->setParent(parentItem);
    56.  
    57. QQmlEngine::setObjectOwnership(childItem, QQmlEngine::CppOwnership);
    58.  
    59. return app.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kafanti; 8th October 2015 at 14:38.

  2. #2
    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: loading QML file as a component from C++ to QQmlApplicationEngine

    Can you explain what you are trying to do and what does not work?

    Cheers,
    _

Similar Threads

  1. Replies: 15
    Last Post: 14th September 2015, 13:42
  2. QQuickView or QQmlApplicationEngine or QQuickWidget
    By ustulation in forum Qt Quick
    Replies: 0
    Last Post: 18th January 2015, 13:16
  3. QQmlApplicationEngine crashing (QT5 and VS 2010)
    By leonardo.massei in forum Qt Programming
    Replies: 0
    Last Post: 28th January 2014, 13:09
  4. component from file
    By codeman in forum Qt Quick
    Replies: 3
    Last Post: 13th January 2014, 17:06
  5. Replies: 3
    Last Post: 2nd August 2012, 13: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.