Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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:

C++
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;
}
Posted
Updated 26-Oct-15 20:14pm
v2
Comments
nv3 27-Oct-15 12:15pm    
And what exactly do you mean by "I was unable to do so"?
pohcb_sonic 27-Oct-15 20:59pm    
I wasn't able to display the "needle" on top of the "Speedometer."
nv3 28-Oct-15 5:01am    
Do you want to say: Although it thought the DrawNeedle function would paint a needle over the Speedometer scale, the needle did not appear and just the speedometer scale appeared, unmodified. Is that what you observed? What have you tried to resolve that? Have you tried leave out the rotation step and just display the needle unrotated?
pohcb_sonic 29-Oct-15 22:05pm    
Yes, exactly what I'm trying to say.
So far, I managed to get the background speedometer to appear, but not the needle on the foreground. And, I also need the needle to rotate.

1 solution

At first you should load the pictures only once, best as members of your Emu class.

The drawing may not work because of coordinate issues. Try to only "draw the needle in your haystack" ;-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900