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
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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();
}