Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. adding QWidget to Quick based application
Forum Updated to NodeBB v4.3 + New Features

adding QWidget to Quick based application

Scheduled Pinned Locked Moved Solved The Lounge
13 Posts 8 Posters 2.9k Views 4 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.
  • M mzimmers
    6 Aug 2023, 20:32

    Hi all -

    I'm implementing a desktop app using QML. I finally ran across something available in a QWidget that doesn't seem to have a QML wrapper.

    I've done web searches, but haven't found anything on adding/using a QWidget class to QML code. I know that in years past, this was pretty much a non-starter, but I'm wondering whether there's anything in 6 that makes this easier.

    Thanks for any information...

    A Offline
    A Offline
    Axel Spoerl
    Moderators
    wrote on 7 Aug 2023, 07:20 last edited by
    #2

    @mzimmers
    You may want to check the documentation of QQuickItemfor that purpose.

    Software Engineer
    The Qt Company, Oslo

    M 1 Reply Last reply 7 Aug 2023, 15:39
    2
    • A Axel Spoerl
      7 Aug 2023, 07:20

      @mzimmers
      You may want to check the documentation of QQuickItemfor that purpose.

      M Offline
      M Offline
      mzimmers
      wrote on 7 Aug 2023, 15:39 last edited by mzimmers 8 Jul 2023, 16:01
      #3

      @Axel-Spoerl this looks like it's what I need. I've created a class:

      class DatePicker : public QQuickItem
      {
          Q_OBJECT
          QML_ELEMENT
          QCalendarWidget calendar;
          ...
      

      and registered it:

          qmlRegisterType<DatePicker>("DatePicker", 1, 0, "DatePicker");
      

      and I attempt to use it:

      Window {
          visible: true
          DatePicker {
              anchors.fill: parent
          }
      

      But nothing shows up. What am I missing?

      EDIT:

      I also gave it a size:

      DatePicker::DatePicker()
      {
          QSizeF size(300, 300);
          setSize(size);
      }
      

      Also, I needed to use QApplication instead of QGuiApplication, because the latter gave me a runtime error:

      QWidget: Cannot create a QWidget without QApplication
      

      I'd greatly prefer to use QGuiApplication; is this possible?

      Thanks...

      C 1 Reply Last reply 7 Aug 2023, 17:31
      0
      • M mzimmers
        7 Aug 2023, 15:39

        @Axel-Spoerl this looks like it's what I need. I've created a class:

        class DatePicker : public QQuickItem
        {
            Q_OBJECT
            QML_ELEMENT
            QCalendarWidget calendar;
            ...
        

        and registered it:

            qmlRegisterType<DatePicker>("DatePicker", 1, 0, "DatePicker");
        

        and I attempt to use it:

        Window {
            visible: true
            DatePicker {
                anchors.fill: parent
            }
        

        But nothing shows up. What am I missing?

        EDIT:

        I also gave it a size:

        DatePicker::DatePicker()
        {
            QSizeF size(300, 300);
            setSize(size);
        }
        

        Also, I needed to use QApplication instead of QGuiApplication, because the latter gave me a runtime error:

        QWidget: Cannot create a QWidget without QApplication
        

        I'd greatly prefer to use QGuiApplication; is this possible?

        Thanks...

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 7 Aug 2023, 17:31 last edited by Christian Ehrlicher 8 Jul 2023, 17:32
        #4

        @mzimmers said in adding QWidget to Quick based application:

        I'd greatly prefer to use QGuiApplication; is this possible?

        Since you already found out that a QWidget needs a QApplication - no.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        M 1 Reply Last reply 8 Aug 2023, 16:28
        1
        • C Christian Ehrlicher
          7 Aug 2023, 17:31

          @mzimmers said in adding QWidget to Quick based application:

          I'd greatly prefer to use QGuiApplication; is this possible?

          Since you already found out that a QWidget needs a QApplication - no.

          M Offline
          M Offline
          mzimmers
          wrote on 8 Aug 2023, 16:28 last edited by
          #5

          @Christian-Ehrlicher OK, that's fine. I've modified my program to use QApplication, and in my QQuickItem c'tor, I've added a call to calendar.show().

          Now, the calendar appears, but in a separate window. I suppose this is expected, as I create it without a parent, which according to the docs, a nullptr parent causes the c'tor to create a (new) window.

          Given that I'm defining my Window in my Main.qml file, I'm not sure how to give the calendar the appropriate parent. Is there away to do this, or must I define my main window using a QQuickItem as well?

          Thanks...

          J 1 Reply Last reply 8 Aug 2023, 18:16
          0
          • M mzimmers
            8 Aug 2023, 16:28

            @Christian-Ehrlicher OK, that's fine. I've modified my program to use QApplication, and in my QQuickItem c'tor, I've added a call to calendar.show().

            Now, the calendar appears, but in a separate window. I suppose this is expected, as I create it without a parent, which according to the docs, a nullptr parent causes the c'tor to create a (new) window.

            Given that I'm defining my Window in my Main.qml file, I'm not sure how to give the calendar the appropriate parent. Is there away to do this, or must I define my main window using a QQuickItem as well?

            Thanks...

            J Offline
            J Offline
            JoeCFD
            wrote on 8 Aug 2023, 18:16 last edited by JoeCFD 8 Aug 2023, 18:17
            #6

            @mzimmers
            from @kkoehne here: https://forum.qt.io/topic/92859/how-to-embed-qwidget-into-qml/2
            It is only supported it the other way round: Embedding a Qt Quick scene in QWidgets, either through QQuickView, QQuickWidget, or QWidget::createWindowContainer().

            Ask @kkoehne about it. The post is old.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 Aug 2023, 19:11 last edited by
              #7

              Hi,

              KDAB has created the DeclarativeWidgets project to integrate widgets in QtQuick applications.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply 8 Aug 2023, 19:31
              1
              • S SGaist
                8 Aug 2023, 19:11

                Hi,

                KDAB has created the DeclarativeWidgets project to integrate widgets in QtQuick applications.

                M Offline
                M Offline
                mzimmers
                wrote on 8 Aug 2023, 19:31 last edited by
                #8

                @SGaist @JoeCFD thanks for the information.

                That KDAB project is huge, and it doesn't appear to have been updated recently (which might be OK).

                If this truly is such a major effort, I suppose I'm better off just implementing my own date picker QML component. I have to say that I'm a bit surprised that Qt doesn't have one.

                J 1 Reply Last reply 8 Aug 2023, 19:41
                0
                • M mzimmers
                  8 Aug 2023, 19:31

                  @SGaist @JoeCFD thanks for the information.

                  That KDAB project is huge, and it doesn't appear to have been updated recently (which might be OK).

                  If this truly is such a major effort, I suppose I'm better off just implementing my own date picker QML component. I have to say that I'm a bit surprised that Qt doesn't have one.

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Aug 2023, 19:41 last edited by JonB 8 Aug 2023, 19:44
                  #9

                  @mzimmers said in adding QWidget to Quick based application:

                  implementing my own date picker QML component. I have to say that I'm a bit surprised that Qt doesn't have one

                  Remember I know nothing about QML. Or whether "QtQuick" matters. But why aren't:

                  • https://doc.qt.io/qt-5/qml-qtquick-controls-calendar.html
                  • https://felgo.com/doc/felgo-datepicker/
                  • https://stackoverflow.com/questions/74930721/how-to-build-a-scrollable-date-picker-in-qml

                  etc. "QML date pickers"? Hmm, maybe "QML component" isn't any good for "QtQuick" ....

                  M B 2 Replies Last reply 8 Aug 2023, 19:48
                  0
                  • J JonB
                    8 Aug 2023, 19:41

                    @mzimmers said in adding QWidget to Quick based application:

                    implementing my own date picker QML component. I have to say that I'm a bit surprised that Qt doesn't have one

                    Remember I know nothing about QML. Or whether "QtQuick" matters. But why aren't:

                    • https://doc.qt.io/qt-5/qml-qtquick-controls-calendar.html
                    • https://felgo.com/doc/felgo-datepicker/
                    • https://stackoverflow.com/questions/74930721/how-to-build-a-scrollable-date-picker-in-qml

                    etc. "QML date pickers"? Hmm, maybe "QML component" isn't any good for "QtQuick" ....

                    M Offline
                    M Offline
                    mzimmers
                    wrote on 8 Aug 2023, 19:48 last edited by
                    #10

                    @JonB thanks for the update - the last time I looked, Qt 6 didn't have the QML calendar (at least it wasn't documented in the normal location). Now, it appears that it does. I can probably use that and the MonthGrid, and a few other things to make my own (essentially what the SO article you referenced does). The felgo example doesn't look right for me -- I need a real calendar.

                    I'll start playing with the tools I'm given...thanks again.

                    1 Reply Last reply
                    0
                    • J JonB
                      8 Aug 2023, 19:41

                      @mzimmers said in adding QWidget to Quick based application:

                      implementing my own date picker QML component. I have to say that I'm a bit surprised that Qt doesn't have one

                      Remember I know nothing about QML. Or whether "QtQuick" matters. But why aren't:

                      • https://doc.qt.io/qt-5/qml-qtquick-controls-calendar.html
                      • https://felgo.com/doc/felgo-datepicker/
                      • https://stackoverflow.com/questions/74930721/how-to-build-a-scrollable-date-picker-in-qml

                      etc. "QML date pickers"? Hmm, maybe "QML component" isn't any good for "QtQuick" ....

                      B Offline
                      B Offline
                      Bob64
                      wrote on 9 Aug 2023, 23:05 last edited by Bob64 8 Oct 2023, 08:41
                      #11

                      @JonB your first bullet point is the closest to anything officially supported by Qt. However it is in Quick Controls 1 which got deprecated towards the end of Qt 5 and was removed entirely from Qt 6. In Qt 5 we were encouraged to move to Quick Controls 2 but it lacked a lot of basic functionality. It never offered even provided a tree view in Qt 5 for example - that only appeared relatively recently in Qt 6.

                      Edit: For some reason mzimmers' reply wasn't visible when I wrote this. It looks like Qt 6 does have a QML calendar after all - possibly a recent addition.

                      M 1 Reply Last reply 10 Aug 2023, 13:37
                      1
                      • B Bob64
                        9 Aug 2023, 23:05

                        @JonB your first bullet point is the closest to anything officially supported by Qt. However it is in Quick Controls 1 which got deprecated towards the end of Qt 5 and was removed entirely from Qt 6. In Qt 5 we were encouraged to move to Quick Controls 2 but it lacked a lot of basic functionality. It never offered even provided a tree view in Qt 5 for example - that only appeared relatively recently in Qt 6.

                        Edit: For some reason mzimmers' reply wasn't visible when I wrote this. It looks like Qt 6 does have a QML calendar after all - possibly a recent addition.

                        M Offline
                        M Offline
                        mzimmers
                        wrote on 10 Aug 2023, 13:37 last edited by
                        #12

                        @Bob64 said in adding QWidget to Quick based application:

                        It looks like Qt 6 does have a QML calendar after all - possibly a recent addition.

                        Yeah - I think originally it was part of the labs stuff, then it went away, then (recently) it reappeared as part of "mainstream" Qt.

                        1 Reply Last reply
                        0
                        • M mzimmers has marked this topic as solved on 15 Aug 2023, 20:52
                        • B Offline
                          B Offline
                          baburaoz558
                          wrote on 9 Feb 2024, 13:13 last edited by
                          #13

                          Perhaps a preview toggle would be useful as to prevent clutter while choosing layout while still having the option to see the result before confirming. I would recommend using icons instead of words for the preview toggle and confirmation button as the idea's theme is about order and aesthetics.

                          1 Reply Last reply
                          0

                          11/13

                          9 Aug 2023, 23:05

                          • Login

                          • Login or register to search.
                          11 out of 13
                          • First post
                            11/13
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved