简体   繁体   中英

QWT Dial, show unit

I try do speedometer in QT 5.8. I use widget QWT Dial. But I have problem as show a label for unit (km/h). I used google, but I don't find it. How Can I do it? thank you

The solution I propose is to create a class that inherits from QwtDial and overwrite the paintEvent method.

mydial.h

#ifndef MYDIAL_H
#define MYDIAL_H

#include <qwt_dial.h>

class myDial : public QwtDial
{
    Q_OBJECT
public:
    myDial(QWidget *parent = NULL );

protected:
    void paintEvent(QPaintEvent *event);
};

#endif // MYDIAL_H

mydial.cpp

#include "mydial.h"

#include <QPainter>

myDial::myDial(QWidget *parent):QwtDial(parent)
{

}

void myDial::paintEvent(QPaintEvent *event)
{

    QwtDial::paintEvent(event);
    QPainter painter(this);
    painter.setPen(Qt::black);
    QFont font;
    font.setPointSize(11);
    painter.setFont(font);
    QString text = QString("%1 km/h").arg(value());
    auto c = rect().center();
    auto Size = painter.fontMetrics().size(Qt::TextSingleLine, text);
    painter.drawText(QPointF(c.x() -Size.width()/2, c.y()+5*Size.height()), text);

}

Example:

#include <QApplication>
#include "mydial.h"
#include <qwt_dial_needle.h>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    myDial w;
    w.setScaleArc(30,330);
    w.setLineWidth(15);
    w.setWrapping(false);
    QwtDialSimpleNeedle *nd = new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Arrow, Qt::white, Qt::red);
    w.setNeedle(nd);
    w.setWindowTitle("Custom QwtDial");

    w.show();

    return a.exec();
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM