Results 1 to 4 of 4

Thread: Can't refresh QLabel's Pixmap properly

  1. #1
    Join Date
    Jun 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Can't refresh QLabel's Pixmap properly

    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!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can't refresh QLabel's Pixmap properly

    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.

  3. #3
    Join Date
    Jun 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't refresh QLabel's Pixmap properly

    THANK YOU!!!!! I'll never make that mistake again.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can't refresh QLabel's Pixmap properly

    We have all been there

Similar Threads

  1. refresh QLabel text
    By dragonprog in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2015, 07:57
  2. refresh image in a Qlabel
    By prkhr4u in forum Newbie
    Replies: 12
    Last Post: 22nd October 2013, 08:49
  3. QLabel->pixmap not scaling properly and breaking the layout
    By Jon Heron in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 21st November 2010, 16:54
  4. Pixmap updating QLabel
    By Matt in forum Newbie
    Replies: 11
    Last Post: 17th August 2010, 22:11
  5. empty pixmap as a QLabel
    By tommy in forum Qt Programming
    Replies: 16
    Last Post: 11th December 2007, 22:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.