Adding QML to widget



  • Hi Can you please help me how to add a qml form to a widget (say a dialog)? I tried to create a container and use QQuickView. But I have trouble using the addwidget function on the dialog. It says it's not a member function. Below is the coding:

    QQuickView *view = new QQuickView();
    QWidget *container = QWidget::createWindowContainer(view, this);
    
    container->setMinimumSize(250, 250);
    container->setMaximumSize(250, 250);
    container->setFocusPolicy(Qt::TabFocus);
    
    view->setSource(QUrl("test.qml"));
    ui->addWidget(container);


  • What is that ui class ? By your post it seems that it does not have addwidget function. What is your use case ? Where do u want add Qml ? What is ui class ?


  • Moderators

    @pdsc_dy said in Adding QML to widget:

    Hi Can you please help me how to add a qml form to a widget (say a dialog)?

    Add a QQuickWidget to your dialog. You don't need QQuickView or createWindowContainer().

    But I have trouble using the addwidget function on the dialog. It says it's not a member function.

    Well, ui is not the dialog.



  • QVBoxLayout *l = new QVBoxLayout;
    l->addWidget(quickWidgetOrContainer);
    setLayout(l);
    


  • @dheerendra @JKSH @mtrch Thank you all for the advice. I changed the coding to the following:

    QQuickWidget *quickWidget = new QQuickWidget; 
    quickWidget->setSource(QUrl::fromLocalFile("test.qml"));
    QVBoxLayout *l = new QVBoxLayout;
    l->addWidget(quickWidget);
    setLayout(l);
    

    All these happen inside the cpp file of a qdialog. Now the model build and run without errors. It does show a blank square but it covers couple of buttons I have on the dialog. The QML form does not show up either. Can you let me know what I have missed?

    Another side question, when you do setLayout this way, does it imply it the setLayout is applied to the dialog? I can ignore the pointer "this" in this way? Thanks.


  • Moderators

    @pdsc_dy Have you specified size for the root item ? Since by default the view is resized to its size. Either specify it a size or change resizeMode to QQuickWidget::SizeRootObjectToView.
    More infor here:
    http://doc.qt.io/qt-5/qquickwidget.html#resizeMode-prop


  • Moderators

    @pdsc_dy said in Adding QML to widget:

    Another side question, when you do setLayout this way, does it imply it the setLayout is applied to the dialog? I can ignore the pointer "this" in this way? Thanks.

    Yes. When you call a class member function from another member function, this is implied.



  • Thank you all. It worked!


Log in to reply
 

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