Extending QML's Component with C++

So this post is about to extend Qt’s QML components. I was in the need of a few extra features for QML’s TextArea component like getting the width of last line from wrapped text and it is not possible with TextArea. So I decided to derive my own class from TextArea’s class and keep going on to my way. But it was kind of possible too. I decided to make reverse engineering and searched for TextArea’s class definition. It was private and by default I wasn’t allowed to play with it.

I’m about to share for both QMake and CMake flags for building it up.
For QMake
QT += quick quick-private quicktemplates2-private
For CMake
find_package(Qt5 COMPONENTS Core Quick QuickTemplates2 REQUIRED)
PRIVATE Qt5::Core Qt5::Quick Qt5::CorePrivate Qt5::QuickPrivate Qt5::QuickTemplates2 Qt5::QuickTemplates2Private)

After unlocking Private API of Qt, you’re able to extend Qt’s classes. But do not forget that, Whenever they Upgrade Qt versions (There will be 5.15 LTS for Qt5 and it will be last version for Qt5, and there’s no guarentee they don’t change it’s source for Qt6 – Late 2020)

In sample class whatever I’ve done is to extend QQuickTextArea class as MKTextArea add 2 simple properties.
Q_PROPERTY(qreal lastLineWidth READ lastLineWidth NOTIFY lastLineWidthChanged)
Q_PROPERTY(bool enableCursorShape READ enableCursorShape WRITE setEnableCursorShape NOTIFY enableCursorShapeChanged)

You can check sample project with QMake over my Github Repository

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *