QLCDNumber for QML

Qt QML does not come with the equivalent of a QLCDNumber. Hence the goal is to integrate the native QLCDNumber Qt widget as best as possible with QML, i.e., give it the look and feel of QML and make it work with the touch UI.

The main idea is to use QQuickPaintedItem class as base class for our item and re-implement a virtual method “paint” in a subclass to provide the item’s painting implementation, using painter object.
For this we can use the existing QLCDNumber class specifically the method paintEvent()

LCDNumber Class Declaration

The LCDNumber class inherits from QQuickPaintedItem. QQuickPaintedItem is the base class for all QPainter based items in the QML Scene Graph framework. I also support some well known properties from the QLCDNumber class:

class LCDNumber : public QQuickPaintedItem
{
   Q_OBJECT
 
   Q_PROPERTY(QString text READ getText WRITE setText NOTIFY textChanged)
   Q_PROPERTY(bool smallDecimalPoint READ smallDecimalPoint WRITE setSmallDecimalPoint)
   Q_PROPERTY(int digitCount READ digitCount WRITE setDigitCount)
   Q_PROPERTY(Mode mode READ mode WRITE setMode)
   Q_PROPERTY(double value READ value WRITE display)
   Q_PROPERTY(int intValue READ intValue WRITE display)
   Q_PROPERTY(QColor lightColor READ lightColor WRITE setLightColor)
   Q_PROPERTY(QColor darkColor  READ darkColor  WRITE setDarkColor)
   Q_PROPERTY(QColor textColor  READ textColor  WRITE setTextColor)

LCDNumber Class Definition

The most important part of the class is paint() function which is automatically called by the Scene Graph framework to paint the contents of the item. The function paints the item in local coordinates. This function and the other local functions were taken from QLCDNumber widget implementation.

void LCDNumber::paint(QPainter *painter)
{
   if (m_text.isEmpty()) 
       return;
 
   Q_D(LCDNumber);
   painter->setRenderHint(QPainter::Antialiasing);
   if (d->shadow)
       painter->translate(0.5, 0.5);
 
   d->digitStr = m_text;
 
   if (d->smallPoint)
       d->drawString(d->digitStr, *painter, &d->points, false);
   else
       d->drawString(d->digitStr, *painter, 0, false);
}

After building the project and deploying the plugin, we can use our new QML LCDNumber control as follows:

import QtQuick 2.12
import QtQuick.Controls 2.12
import LCDNumberPlugin 1.0

ApplicationWindow {
   id: mainWin
   visible: true
Boosts Confidence Men who suffer from erectile dysfunction  viagra viagra sildenafil or impotency may have low self-esteem. According to reliable sources, generic drugs save consumers an estimated 8 to 10 billions USD a year at retail pharmacies. best tadalafil It results from blood being trapped in the penile tissue. get free viagra Kamagra is a great mixture of sildenafil citrate or the credit always goes to this chemical.  viagra online no prescription    width: 640
   height: 480
   title: qsTr("LCDNumber Test")

   Column {
       spacing: 40
       anchors.fill: parent

       LCDNumber {
           id: lcd1
           width: parent.width
           height: parent.height * 0.3
           text: "00:00:00"
           digitCount: 8
           textColor: "red"
       }

       LCDNumber {
           id: lcd2
           width: parent.width
           height: parent.height * 0.2
           text: "00:00:00"
           digitCount: 8
           textColor: "blue"
           smallDecimalPoint: true
       }

       LCDNumber {
           id: lcd3
           width: parent.width
           height: parent.height * 0.1
           text: "00:00:00"
           digitCount: 8
           textColor: "green"
           smallDecimalPoint: true
       }
   }

   Timer {
       interval: 1000; running: true; repeat: true
       onTriggered: {
           lcd1.text = Qt.formatTime(new Date(),"hh:mm:ss")
           lcd2.text = lcd1.text
           lcd3.text = lcd1.text
           lcd1.update()
           lcd2.update()
           lcd3.update()
       }
   }
}

The source code could be found at the git repository here…

Running test