[SOLVED]Best method to display dynamic images
-
Trying to create a image that is changed once a button is pressed. Currently able to display an image fine using setpixmap.
However what I am wondering is, is this the preferred method for displaying images ?
Essentially what I am trying to achieve is be able to change an image displayed based on conditions, so I have created a test program below. However I am not sure how to proceed to make the image change. I thought maybe loading two pixmaps into the heap and then setting a QLabel (don't care if it is a QLabel just want to display an image inside a widget) using setpixmap, however to me this seems like an inefficient method (there is never a time when both are being displayed only one at a time). Am I going in the right direction or should I be looking at other methods ?
@#include "widget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QPixmap image1("C:\Users\Documents\QPixmap\radaroff.png");
QPixmap image2("C:\Users\Documents\QPixmap\radaron.png");
QVBoxLayout * layout = new QVBoxLayout(this);
QLabel * Label = new QLabel(this);
QPushButton * Button = new QPushButton(this);
Label->setPixmap(image1);
layout->addWidget(Label);
layout->addWidget(Button);
}Widget::~Widget()
{}
@@#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include "gui_image.h"
class Widget : public QWidget
{
Q_OBJECTpublic:
Widget(QWidget *parent = 0);
~Widget();gui_image * Main_Image;
};
#endif // WIDGET_H
@@#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();return a.exec();
}
@Also there will only be a maximum of 3 images so if there is a better method please let me know.
-
Hi,
That looks correct for you purpose.
On a side note, you should use the unix notation for paths. Less likely to write wrong things
-
Yea good point SGaist didn't think of that btw when I was coding this by mistake wrote this.
QPixmap fImage = filename_off
where as filename_off is a QString to the file being used by the pixmap, which actually worked to my surprise. So checked the documentation for an overloaded operator that took a QString and couldn't find one. So even though it wasn't in the documentation i'm presuming:
QPixmap fImage = filename_off
is the same as
QPixmap fImage = QPixmap(filename_off)
?? Am I right in my thinking?
-
AFAIK, the third constructor is used here to construct the pixmap since the two last parameters have default values.