qml tickmark values of custom Gauge
Solved
QML and Qt Quick
-
I'am trying to use this code to display a custom Gauge but the tickmak values does not shown correctly!
Thanks in advance
main.qmlimport QtQuick 2.4 import QtQuick.Extras 1.4 import QtQuick.Layouts 1.0 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4 Rectangle { width: 50 height: 300 color: "transparent" property alias gauge: gauge_act_1 Gauge { id: gauge_act_1 minorTickmarkCount: 2 font.pixelSize: 9 anchors.centerIn: parent anchors.leftMargin: 2 anchors.rightMargin: 2 style: GaugeStyle { valueBar: Rectangle { color: "#FF003399" implicitWidth: 15 } } objectName: "gauge_act_1" } }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QQuickView *view = new QQuickView; view->setSource(QUrl("qrc:/main.qml")); view->setColor(QColor(Qt::white)); view->setResizeMode(QQuickView::SizeRootObjectToView); QWidget *container = QWidget::createWindowContainer(view); container->setFocusPolicy(Qt::TabFocus); container->setMinimumSize(QSize(400,330)); container->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); ui->horizontalLayout->addWidget(container); QObject *object = view->rootObject(); QObject* item_1 = object->findChild<QObject *>("gauge_act_1"); if(item_1) gauge_1 = item_1; QTimer *G_s_qt_controllerTimer = new QTimer; QObject::connect(G_s_qt_controllerTimer, SIGNAL( timeout()),this, SLOT(update())); G_s_qt_controllerTimer->start(100); } void MainWindow::update(){ gauge_1->setProperty("maximumValue", 481.9); gauge_1->setProperty("minimumValue", 357.1); gauge_1->setProperty("value", 410.84); } MainWindow::~MainWindow() { delete ui; } void MainWindow::changeEvent(QEvent *e) { QMainWindow::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; } }
-
How about that ?
Rectangle { width: 50 height: 300 anchors.centerIn: parent color: "transparent" property alias gauge: gauge_act_1 Gauge { id: gauge_act_1 minorTickmarkCount: 2 font.pixelSize: 9 anchors.centerIn: parent anchors.leftMargin: 2 anchors.rightMargin: 2 maximumValue : 481.9 minimumValue: 357.1 formatValue: function(value) { return value.toFixed(1); } value : 381.2 style: GaugeStyle { valueBar: Rectangle { color: "#FF003399" implicitWidth: 15 } } objectName: "gauge_act_1" } }
The important part here is this which format your label with 1 decimal :
formatValue: function(value) { return value.toFixed(1); }
Here the related doc : formatValue