There is no denying that QtQuick has a lot of handy features and allows programmers to build nice GUI applications with ease. However, sometimes we would like to integrate the functionality of "legacy" Qt Widgets with our QML applications.
For example, QtQuick does not have a direct replacement of the QPlainTextEdit widget (which is very useful if you need to log large amounts of data or if you want to build a custom code editor). Other examples of such widgets are the QTableWidget and third-party widgets (such as Qwt or QScintilla).
I bumped into this issue while developing an appropriate way to create a QML-friendly serial output console for Serial Studio. Using the standard QML TextArea resulted too slow for displaying high-frequency data from a serial port device.
To fix this, I decided to use the QPlainTextEdit widget for displaying incoming serial data and use QQuickPaintedItem to render the widget in the QML interface.
We'll begin our unorthodox programming ritual with the following header code:
And here is the implementation code for this class:
Note: in the processMouseEvents() and processWheelEvents() functions, we are writing potentially illegal C++ code because we are indirectly accessing protected functions of the QPlainTextEdit class. Unfortunately, this is the only way that I managed to get the widget to accept mouse/wheel events. If you have a better solution, you are welcome to share it :)
To use this class from the QML interface, we need to add the following code in the main() function (or before initializing the QML engine):
Finally, here is an example of using QmlPlainTextEdit from QML:
You can also add functions to read/write widget properties from QML using the Q_PROPERTY macro. You can check the complete QmlPlainTextEdit code in the following links:
Please take into account that these files are specific to Serial Studio, so you will need to modify them to use them in a "portable" manner in your projects.
Finally, you can follow this approach with any QWidget based object that you need to use from your QML interface. I have used this approach with a QTableWidget for a proprietary project without any major issues.