I'm trying to display and rotate an image within another image in the QLabel in QT Creator 5.5. However, I was unable to do so. The project was in a UI form format.
"lblVehSpd" is a Qlabel from the interactive UI.
the following are the codes:
In header file emu.h:
#include <QMainWindow>
#include <QApplication>
#include "PCANLight.h"
#include "Elements.h"
#include "iostream.h"
#include <QTextStream>
#include <QTimer>
#include <QStyleFactory>
#include <QImage>
#include <QPainter>
#include <QTransform>
#include <QPainterPath>
#include <QPixmap>
#include <QLabel>
#include <QStylePainter>
namespace Ui {
class Emu;
}
class Emu : public QMainWindow
{
Q_OBJECT
public:
explicit Emu(QWidget *parent = 0);
~Emu();
PCANLight pcan;
Elements e;
void DrawBase(QPainter *painter);
void DrawNeedle(QPainter *painter);
private slots:
void UpdateCompass();
private:
Ui::Emu *ui;
void SpeedOMeter(QWidget *parent);
void paintEvent(QPaintEvent *ev);
QTimer *CompassTime;
};
In cpp file emu.cpp:
Emu::Emu(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Emu)
{
SpeedOMeter(this);
}
void Emu::SpeedOMeter(QWidget *parent)
{
QImage BaseImage(":/speedometer.png");
ui->lblVehSpd->setPixmap(QPixmap::fromImage(BaseImage));
CompassTime = new QTimer(this);
connect(CompassTime, SIGNAL(timeout()), this, SLOT(UpdateCompass()));
CompassTime->start(1000);
}
void Emu::paintEvent(QPaintEvent *ev)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
p.save();
p.translate(400, 303);
DrawBase(&p);;
p.restore();
p.save();
p.translate(ui->lblVehSpd->width() + 68, ui->lblVehSpd->height()+68);
p.rotate(90.0);
DrawNeedle(&p);;
p.restore();
}
void Emu::DrawBase(QPainter *painter)
{
QPixmap Base(":/speedometer.png");
painter->drawPixmap(0,0,400,303, Base);
}
void Emu::DrawNeedle(QPainter *painter)
{
QPixmap Needle(":/needle.png");
painter->drawPixmap(200,152,ui->lblVehSpd->width(),ui->lblVehSpd->height(), Needle);
}
void Emu::UpdateCompass()
{
update();
}
Emu::~Emu()
{
delete ui;
}