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. [SOLVED]Show qml file in qwidget
Qt 6.11 is out! See what's new in the release blog

[SOLVED]Show qml file in qwidget

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.6k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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);
        }
        

        @

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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();
          @

          (Z(:^

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              [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 :-)

              (Z(:^

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  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.

                  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