题目:使用图片绘制出仪表盘
代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);void paintEvent(QPaintEvent* event) override;~Widget();
public slots:void horizontalSliderValueChangedSlot();private:Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);ui->horizontalSlider->setMaximum(100);ui->horizontalSlider->setMinimum(0);connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(horizontalSliderValueChangedSlot()));
}void Widget::paintEvent(QPaintEvent *event)
{int size = 250;QPainter p(this);p.translate(this->width()/2,this->height()/2);p.drawPixmap(-size,-size,2*size,2*size,QPixmap(":/n/rs/Pan.png"));p.rotate(130+2.8*ui->horizontalSlider->value());p.drawPixmap(-27,-20,200,40,QPixmap(":/n/rs/Point.png"));
}Widget::~Widget()
{delete ui;
}void Widget::horizontalSliderValueChangedSlot()
{update();
}
运行效果: