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] How to displaying QML file in widget

[SOLVED] How to displaying QML file in widget

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

    Hello ;

    Who can help me to show a qml file on my widget
    after run qml file show for me but qml content no ! where is problem ?

    This is Widget code for show my qml file
    @
    QDeclarativeView *qmlView = new QDeclarativeView;
    qmlView->setSource(QUrl::fromLocalFile("kk.qml"));
    qmlView->show();
    ui->gridLayoutQ->addWidget(qmlView);
    @

    and this is qml file :

    @
    import QtQuick 2.0

    Rectangle {
    width: 100
    height: 62

    Text {
        id: text1
        x: 36
        y: 24
        text: qsTr("<b>Text</b>")
        font.pixelSize: 12
    }
    

    }

    @

    file.pro

    @
    #-------------------------------------------------

    Project created by QtCreator 2014-05-07T10:02:40

    #-------------------------------------------------

    QT += core gui qml quick declarative

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = untitled24
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    OTHER_FILES +=
    kk.qml

    @

    Qt version : 5.2 and 5.3
    Os : Linux / Windows

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

      Hi,

      QDeclarativeView is for Qt Quick 1.

      For Qt Quick 2, you have 2 options:

      • Use QQuickView and QWidget::createWindowContainer(): https://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/ (Requires Qt 5.1 and above)
      • Use "QQuickWidget":http://doc-snapshot.qt-project.org/qt5-release/qquickwidget.html (Requires Qt 5.3 and above)

      [quote author="Kamb!z" date="1399448588"]
      QT += core gui qml quick declarative[/quote]Remove "declarative". That is for Qt Quick 1 only.

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

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

        [quote author="JKSH" date="1399449048"]Hi,

        QDeclarativeView is for Qt Quick 1.

        For Qt Quick 2, you have 2 options:

        • Use QQuickView and QWidget::createWindowContainer(): https://blog.qt.digia.com/blog/2013/02/19/introducing-qwidgetcreatewindowcontainer/ (Requires Qt 5.1 and above)
        • Use "QQuickWidget":http://doc-snapshot.qt-project.org/qt5-release/qquickwidget.html (Requires Qt 5.3 and above)

        [quote author="Kamb!z" date="1399448588"]
        QT += core gui qml quick declarative[/quote]Remove "declarative". That is for Qt Quick 1 only.[/quote]

        Hi again and thank you for reply but after run just show black screen ! why ?

        code :

        @
        QQuickWidget *view = new QQuickWidget;
        view->setSource(QUrl::fromLocalFile("kk.qml"));
        view->show();
        @

        check this screen shoot please : http://persianupload.com/kleeja/do.php?imgf=139945002257841.jpg

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

          And again after remove declarative ...

          And this : http://persianupload.com/kleeja/do.php?imgf=139945042555611.jpg

          Why content is empty !!!??

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            Are you using an OpenGL version of Qt, or an ANGLE version?

            Does your graphics card support OpenGL 2.0?

            In Qt Creator, start a new Qt Quick project:

            File -> New File or Project... -> Applications -> Qt Quick Application -> Qt Quick 2.2

            Click "Run. What do you see?

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

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

              [quote author="JKSH" date="1399458692"]# Are you using an OpenGL version of Qt, or an ANGLE version?

              Does your graphics card support OpenGL 2.0?

              In Qt Creator, start a new Qt Quick project:

              File -> New File or Project... -> Applications -> Qt Quick Application -> Qt Quick 2.2

              Click "Run. What do you see?

              [/quote]

              It's Angle version , not OpenGL
              About My GPU : (AMD Readon HD 6670) OPENGL 4.1 SUPPORT : Yes
              In Qt Creator I have Qt Quick 2.2
              After create and run this is result : http://persianupload.com/kleeja/do.php?imgf=13994592497651.jpg

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="Kamb!z" date="1399459302"]After create and run this is result : http://persianupload.com/kleeja/do.php?imgf=13994592497651.jpg[/quote]Good, it's working :)

                If you still want to use QWidgets, add "QT += widgets" to your untitled26.pro and try QQuickView/QQuickWidget again.

                EDIT: What version of Qt Creator are you using? Your project looks different

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

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

                  [quote author="JKSH" date="1399459696"][quote author="Kamb!z" date="1399459302"]After create and run this is result : http://persianupload.com/kleeja/do.php?imgf=13994592497651.jpg[/quote]Good, it's working :)

                  If you still want to use QWidgets, add "QT += widgets" to your untitled26.pro and try QQuickView/QQuickWidget again.

                  EDIT: What version of Qt Creator are you using? Your project looks different[/quote]

                  Unfortunately doesn't work ! :( content of QML file is a string ("Hello world") but isn't show.

                  See : http://persianupload.com/kleeja/do.php?imgf=139946053737491.jpg

                  and my project : http://persianupload.com/kleeja/do.php?filename=139946053748452.zip

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    Your URL is wrong.

                    Look inside main.cpp -- the correct URL is written there.

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

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

                      [quote author="JKSH" date="1399462036"]Your URL is wrong.

                      Look inside main.cpp -- the correct URL is written there.[/quote]

                      This is main address of qml file : qrc:///qml/main.qml

                      view->setSource(QUrl::fromLocalFile("qrc:///qml/main.qml"));

                      can you check my sample of code ! this is : http://persianupload.com/kleeja/do.php?filename=139946053748452.zip

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        [quote author="Kamb!z" date="1399462538"]view->setSource(QUrl::fromLocalFile("qrc:///qml/main.qml"));[/quote]You shouldn't use QUrl::fromLocalFile(), because items in QRC files are not local files. See main.cpp for the correct way of calling QUrl.

                        Also, open main.qml and change Window {} to Rectangle {} -- that will make things easier for you.

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

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

                          Not working for me again ! Can you give me a sample ? please !
                          I want to compare your project with mine.

                          1 Reply Last reply
                          0
                          • JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            @
                            // mainwindow.cpp
                            // Change this...
                            QUrl::fromLocalFile("qrc:///qml/main.qml")

                            // ...to this:
                            QUrl(QStringLiteral("qrc:///qml/main.qml"))
                            @

                            @
                            // main.qml
                            // Change this...
                            Window {
                            ...

                            // ...to this:
                            Rectangle {
                            @

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

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

                              [quote author="JKSH" date="1399468987"]@
                              // mainwindow.cpp
                              // Change this...
                              QUrl::fromLocalFile("qrc:///qml/main.qml")

                              // ...to this:
                              QUrl(QStringLiteral("qrc:///qml/main.qml"))
                              @

                              @
                              // main.qml
                              // Change this...
                              Window {
                              ...

                              // ...to this:
                              Rectangle {
                              @[/quote]

                              Oh yes !!! I understand now ! thank you JKSH :)

                              1 Reply Last reply
                              0
                              • JKSHJ Offline
                                JKSHJ Offline
                                JKSH
                                Moderators
                                wrote on last edited by
                                #15

                                You're welcome :) Happy coding!

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

                                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