Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Porting a custom widget to QML (in a way that's embeddable into QMainWindow)?
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.
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.
Violet Giraffe last edited by
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.
Embedding Qt Quick GUIs into QWidget-base GUIs is easy, too. There is a "container widget" - QQuickWidget - that is made exactly for this purpose.
Is it OK to use
This post is deleted!