PDA

View Full Version : Can't refresh QLabel's Pixmap properly



deltamac
6th June 2015, 18:02
I have been stuck on this issue for 2 days now. I'm using the Qt plugin for VisualStudio2013 on Window 7-64bit.

I have been trying to display a pair of images in Qlabels. I need to manipulate the pixel data regularly, so I store them in QImages, and every time I want to refresh the display I set the QPixmap of a Qlabel. The problem is, it only seems to refresh if I change/move the window in some way.

This problems goes away if I just make the QLabels children of my QWidget, but never set a layout. If I then add repaint() or update(), the problem comes back.

(this is a very similar post to one I posted using QGraphicsScene, but the problem seems to be more fundamental than that, so I am reposting)

Here is my code. First the .h



#ifndef DISPLAYWIDGET_H
#define DISPLAYWIDGET_H

#include <QtWidgets/QWidget>
#include <qpixmap.h>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QPushButton>
#include <QHBoxLayout>
#include <qtimer.h>
#include <qlabel.h>


#define FULLSCALE 255
#define IM_X_MIN -5.0
#define IM_X_MAX 5.0
#define IM_Z_MIN 0.0
#define IM_Z_MAX 15.0
#define IM_PIXEL_WIDTH 200
#define IM_PIXEL_HEIGHT IM_PIXEL_WIDTH * (IM_Z_MAX-IM_Z_MIN)/(IM_X_MAX - IM_X_MIN)
#define BORDER_WIDTH 10
#define RAND_SEED 7

class DisplayWidget : public QWidget
{
Q_OBJECT

public:
DisplayWidget(int width, int height, QWidget *parent = 0);
~DisplayWidget();

private:
QLabel *bimageLabel;
QLabel *dimageLabel;
QImage * bImage;
QImage * dImage;
QTimer * frameGrab;

QPushButton * debugButton;

void CreateWidgets();
void SetupGui();

int w, h;

public slots:
void GenerateNewData();
};

#endif // DISPLAYWIDGET_H

and the .cpp.



#include "displaywidget.h"

DisplayWidget::DisplayWidget(int width, int height, QWidget *parent): QWidget(parent)
{
//ui.setupUi(this);

w = width;
h = height;

CreateWidgets();
SetupGui();

// seed the random number generator
srand(RAND_SEED);
GenerateNewData();
}

DisplayWidget::~DisplayWidget()
{
}
void DisplayWidget::CreateWidgets()
{
bImage = new QImage(w, h, QImage::Format_ARGB32);
dImage = new QImage(w, h, QImage::Format_ARGB32);
bimageLabel = new QLabel(this);
dimageLabel = new QLabel(this);

debugButton = new QPushButton("DEBUG", this);
bimageLabel->setStyleSheet("QLabel {background-color: black};");
dimageLabel->setStyleSheet("QLabel {background-color: white};");

frameGrab = new QTimer(this);
}
void DisplayWidget::SetupGui()
{
QHBoxLayout * layout = new QHBoxLayout();
setLayout(layout); // commenting this line out makes it refresh
layout->addWidget(bimageLabel);
layout->addWidget(dimageLabel);
layout->addWidget(debugButton);

connect(frameGrab, SIGNAL(timeout()),this, SLOT(GenerateNewData()));
connect(debugButton, SIGNAL(clicked()), this, SLOT(GenerateNewData()));
frameGrab->start(50);
}
void DisplayWidget::GenerateNewData()
{
QRgb * bImageData = (QRgb *)bImage->scanLine(0);
QRgb * dImageData = (QRgb *)dImage->scanLine(0);

for (int i; i < w * h; i++)
{
bImageData[i] = qRgba(rand() % FULLSCALE, 0, 0, FULLSCALE);
dImageData[i] = qRgba(0, 0, rand() % FULLSCALE, FULLSCALE);
}

bimageLabel->setPixmap(QPixmap::fromImage(*bImage).scaled(QSize (IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));
dimageLabel->setPixmap(QPixmap::fromImage(*dImage).scaled(QSize (IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));

//this->update(); // this breaks it again
}

Losing my mind here. I have very limited experience with Qt, but I believe I have the right approach.

Please help!

ChrisW67
6th June 2015, 22:43
Line 51,you are not initialising the loop counter to zero. Chances are good that whatever random rubbish is in the variable causes immediate loop termination.

deltamac
7th June 2015, 08:26
THANK YOU!!!!! I'll never make that mistake again.

ChrisW67
7th June 2015, 12:28
We have all been there ;)