题目:使用图片绘制出仪表盘
代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
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();
}
运行效果:
文章来源地址https://www.toymoban.com/news/detail-801076.html
文章来源:https://www.toymoban.com/news/detail-801076.html
到了这里,关于QT第二周周三的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!