Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Porting a custom widget to QML (in a way that's embeddable into QMainWindow)?
Qt 6.12 LTS is out! See what's new in the release blog

Porting a custom widget to QML (in a way that's embeddable into QMainWindow)?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 3.4k Views 2 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    A total QML newbie here, never touched the thing. I have a simple Qt Widgets application that I want to port to Android (while retaining the current desktop functionality and native looks). It compiles and runs on Android as is, although naturally it has lots of bugs there. I could polish it and keep Widgets, but I want to do it the right way - with Qt Quick 2.

    Now, the main problem I'm facing is one widget. It basically paints text and background using specific colors. But it also paints two lines and it can perform fade-out animation on the text (blend into background). Here's the source code, it really is a small piece of code.

    What should I do with it to replicate this functionality in a "native" Qt Quick Android app while avoiding duplicating this widget in mobile and desktop versions of the program?
    Initially I was going to create a QML app from scratch (new thin UI over the same core code) and embed the existing widget there, but apparently it's no longer possible as of Qt Quick 2.0. Can I do the other way around - port this functionality to QML and embed it into QMainWindow? Is there an example that does something similar? Can you name me some keywords I should search for to understand what I need to do?

    Thanks in advance.

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

      Hi! Porting your custom widget to Qt Quick is easy. You can keep almost all of the code. Just, instead of inheriting from QWidget, inherit from QQuickPaintedItem, override void QQuickPaintedItem::paint(QPainter *painter) and move your code from void CReaderView::paintEvent(QPaintEvent*) to that method.

      V 1 Reply Last reply
      1
      • ? A Former User

        Hi! Porting your custom widget to Qt Quick is easy. You can keep almost all of the code. Just, instead of inheriting from QWidget, inherit from QQuickPaintedItem, override void QQuickPaintedItem::paint(QPainter *painter) and move your code from void CReaderView::paintEvent(QPaintEvent*) to that method.

        V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by
        #3

        @Wieland
        That is very interesting, thank you, going to try it right now.

        But what about re-using it in the desktop QMainWindow-based app? It's not the main piece of code in the app, but certainly the main UI element with some logic inside, I would strongly prefer NOT having to duplicate it.

        ? 1 Reply Last reply
        0
        • V Violet Giraffe

          @Wieland
          That is very interesting, thank you, going to try it right now.

          But what about re-using it in the desktop QMainWindow-based app? It's not the main piece of code in the app, but certainly the main UI element with some logic inside, I would strongly prefer NOT having to duplicate it.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Embedding Qt Quick GUIs into QWidget-base GUIs is easy, too. There is a "container widget" - QQuickWidget - that is made exactly for this purpose.

          1 Reply Last reply
          1
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by Violet Giraffe
            #5

            Is it OK to useQPixmap in my QQuickPaintedItem implementation?

            ? 1 Reply Last reply
            0
            • V Violet Giraffe

              Is it OK to useQPixmap in my QQuickPaintedItem implementation?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @Violet-Giraffe sure

              V 1 Reply Last reply
              1
              • ? A Former User

                @Violet-Giraffe sure

                V Offline
                V Offline
                Violet Giraffe
                wrote on last edited by Violet Giraffe
                #7
                This post is deleted!
                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
                • Unsolved