Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Best method to display dynamic images
Forum Update on Monday, May 27th 2025

[SOLVED]Best method to display dynamic images

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ion_knight
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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(&#41;;
    

    }
    @

    Also there will only be a maximum of 3 images so if there is a better method please let me know.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ion_knight
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          AFAIK, the third constructor is used here to construct the pixmap since the two last parameters have default values.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved