Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create component from C++
Qt 6.11 is out! See what's new in the release blog

Create component from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zondar
    wrote on last edited by Zondar
    #1

    Hi,

    I have been trying to create qml component from C++, I created it sucessfully, but it did not appear in window.

    Thanks in advance

    main.qml

    Rectangle {
        color: "white"
    }
    

    MyRect.qml

    Rectangle {
        width: 100
        height: 100
        color: "red"
    }
    

    main.cpp

    #include <QQuickWidget>
    #include <QQmlEngine>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQuickWidget *quickWidget = new QQuickWidget;
        quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
        quickWidget->resize(1200, 700);
        quickWidget->setSource(QStringLiteral("qrc:/main.qml"));
    
        quickWidget->show();
    
        QQmlComponent component(quickWidget->engine(), QUrl("qrc:/MyRect.qml"));
        qDebug() << component.create(quickWidget->rootContext());
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zondar
      wrote on last edited by
      #2

      Solved by setting object parentItem

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved