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

Qt Code:
  1. #ifndef DISPLAYWIDGET_H
  2. #define DISPLAYWIDGET_H
  3.  
  4. #include <QtWidgets/QWidget>
  5. #include <qpixmap.h>
  6. #include <QGraphicsScene>
  7. #include <QGraphicsView>
  8. #include <QGraphicsPixmapItem>
  9. #include <QPushButton>
  10. #include <QHBoxLayout>
  11. #include <qtimer.h>
  12. #include <qlabel.h>
  13.  
  14.  
  15. #define FULLSCALE 255
  16. #define IM_X_MIN -5.0
  17. #define IM_X_MAX 5.0
  18. #define IM_Z_MIN 0.0
  19. #define IM_Z_MAX 15.0
  20. #define IM_PIXEL_WIDTH 200
  21. #define IM_PIXEL_HEIGHT IM_PIXEL_WIDTH * (IM_Z_MAX-IM_Z_MIN)/(IM_X_MAX - IM_X_MIN)
  22. #define BORDER_WIDTH 10
  23. #define RAND_SEED 7
  24.  
  25. class DisplayWidget : public QWidget
  26. {
  27. Q_OBJECT
  28.  
  29. public:
  30. DisplayWidget(int width, int height, QWidget *parent = 0);
  31. ~DisplayWidget();
  32.  
  33. private:
  34. QLabel *bimageLabel;
  35. QLabel *dimageLabel;
  36. QImage * bImage;
  37. QImage * dImage;
  38. QTimer * frameGrab;
  39.  
  40. QPushButton * debugButton;
  41.  
  42. void CreateWidgets();
  43. void SetupGui();
  44.  
  45. int w, h;
  46.  
  47. public slots:
  48. void GenerateNewData();
  49. };
  50.  
  51. #endif // DISPLAYWIDGET_H
To copy to clipboard, switch view to plain text mode 

and the .cpp.

Qt Code:
  1. #include "displaywidget.h"
  2.  
  3. DisplayWidget::DisplayWidget(int width, int height, QWidget *parent): QWidget(parent)
  4. {
  5. //ui.setupUi(this);
  6.  
  7. w = width;
  8. h = height;
  9.  
  10. CreateWidgets();
  11. SetupGui();
  12.  
  13. // seed the random number generator
  14. srand(RAND_SEED);
  15. GenerateNewData();
  16. }
  17.  
  18. DisplayWidget::~DisplayWidget()
  19. {
  20. }
  21. void DisplayWidget::CreateWidgets()
  22. {
  23. bImage = new QImage(w, h, QImage::Format_ARGB32);
  24. dImage = new QImage(w, h, QImage::Format_ARGB32);
  25. bimageLabel = new QLabel(this);
  26. dimageLabel = new QLabel(this);
  27.  
  28. debugButton = new QPushButton("DEBUG", this);
  29. bimageLabel->setStyleSheet("QLabel {background-color: black};");
  30. dimageLabel->setStyleSheet("QLabel {background-color: white};");
  31.  
  32. frameGrab = new QTimer(this);
  33. }
  34. void DisplayWidget::SetupGui()
  35. {
  36. QHBoxLayout * layout = new QHBoxLayout();
  37. setLayout(layout); // commenting this line out makes it refresh
  38. layout->addWidget(bimageLabel);
  39. layout->addWidget(dimageLabel);
  40. layout->addWidget(debugButton);
  41.  
  42. connect(frameGrab, SIGNAL(timeout()),this, SLOT(GenerateNewData()));
  43. connect(debugButton, SIGNAL(clicked()), this, SLOT(GenerateNewData()));
  44. frameGrab->start(50);
  45. }
  46. void DisplayWidget::GenerateNewData()
  47. {
  48. QRgb * bImageData = (QRgb *)bImage->scanLine(0);
  49. QRgb * dImageData = (QRgb *)dImage->scanLine(0);
  50.  
  51. for (int i; i < w * h; i++)
  52. {
  53. bImageData[i] = qRgba(rand() % FULLSCALE, 0, 0, FULLSCALE);
  54. dImageData[i] = qRgba(0, 0, rand() % FULLSCALE, FULLSCALE);
  55. }
  56.  
  57. bimageLabel->setPixmap(QPixmap::fromImage(*bImage).scaled(QSize(IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));
  58. dimageLabel->setPixmap(QPixmap::fromImage(*dImage).scaled(QSize(IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));
  59.  
  60. //this->update(); // this breaks it again
  61. }
To copy to clipboard, switch view to plain text mode 

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

Please help!