[SOLVED]Show qml file in qwidget



  • Hi ,I use qt 5.3 on mac. i want to show qml file in qwidget. look at the codes:

    @
    //mainwindow.cpp

    void MainWindow::createQMLWindow()
    {
    QQmlEngine engine;
    QQmlComponent component = new QQmlComponent(&engine);
    component->loadUrl(QUrl("qrc:///main.qml"));
    if(!component->isReady())
    {
    qDebug() << "Component Error:" << component->errorString();
    }
    QQuickWindow * mywin = qobject_cast<QQuickWindow
    >(component->create()); //the error occures here
    QWidget *container = QWidget::createWindowContainer(mywin , this);
    setCentralWidget(container);
    QVBoxLayout *layout = new QVBoxLayout;
    container->setLayout(layout);
    container->setMinimumSize(200,200);
    container->setMaximumSize(200,200);
    }
    @

    when i run my program , after a few seconds, the program crashed:
    this error: the program has unexpectedly finished.

    please help me to fix it. Thanks

    [edit: cleaned use of coding tags SGaist]


  • Moderators

    engine is deleted when createQMLWindow() goes out of scope - maybe that is the problem.



  • Thanks for your answer. I've tried to move code to main.cpp, but i doesn't work.
    @
    //main.cpp

    MainWindow w;
    QQmlEngine engine;
    QQmlComponent component = new QQmlComponent(&engine);
    component->loadUrl(QUrl("qrc:///main.qml"));
    if(!component->isReady())
    {
    qDebug() << "Component Error:" << component->errorString();
    }
    QQuickWindow * mywin = qobject_cast<QQuickWindow
    >(component->create());
    w.createQMLWindow();
    w.show();

    //mainwindow.cpp
     
    void MainWindow::createQMLWindow(QQuickWindow * mywin)
    {  
      QWidget *container = QWidget::createWindowContainer(mywin , this);
      setCentralWidget(container);
      QVBoxLayout *layout = new QVBoxLayout;
      container->setLayout(layout);
      container->setMinimumSize(200,200);
      container->setMaximumSize(200,200);
    }
    

    @


  • Moderators

    Please put your code between '@' tags here on the forum, otherwise it's hard to read.

    Why not use "QQuickView":http://qt-project.org/doc/qt-5/qquickview.html, which is a QWindow? The code is much simpler there:
    @
    MainWindow w;
    QVBoxLayout *layout = new QVBoxLayout(w);
    w.setLayout(layout);

    QQuickView *view = new QQuickView;
    view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));

    QWidget container *cont = QWidget::createWindowContainer(view, w);
    layout->addWidget(container);
    w.show();
    @



  • If i use QQuickView like you said, i get this error:

    QQuickView only supports loading of root objects that derive from QQuickItem.
    In my qml file i've used window type.


  • Moderators

    [quote author="hasti66" date="1414389434"]If i use QQuickView like you said, i get this error:

    QQuickView only supports loading of root objects that derive from QQuickItem.
    In my qml file i've used window type.[/quote]

    Then that is not necessary, remove Window and use Rectangle instead :-)



  • I used window type in qml because i want to use UIImagePickerController to pick an image from photo library.



  • finally i found another way to use UIImagePickerController in qt5.3 (on mac) without using qml window type.I created a subclass of UIViewController in .mm file and implemented an UIImagePickerController in that and defined a cpp bridge method. So i don't need to embed a qml file in QWidget anymore.
    Thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.