Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding QML to widget
QtWS25 Last Chance

Adding QML to widget

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 3.4k 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.
  • P Offline
    P Offline
    pdsc_dy
    wrote on last edited by
    #1

    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);
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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 ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      P 1 Reply Last reply
      2
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @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.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        2
        • M Offline
          M Offline
          mtrch
          wrote on last edited by
          #4
          QVBoxLayout *l = new QVBoxLayout;
          l->addWidget(quickWidgetOrContainer);
          setLayout(l);
          
          1 Reply Last reply
          0
          • dheerendraD dheerendra

            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 ?

            P Offline
            P Offline
            pdsc_dy
            wrote on last edited by pdsc_dy
            #5

            @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.

            JKSHJ 1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @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

              157

              1 Reply Last reply
              0
              • P pdsc_dy

                @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.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @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.

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pdsc_dy
                  wrote on last edited by
                  #8

                  Thank you all. It worked!

                  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