c++qt

Paint event not triggered


I am attempting to redraw a widget whenever repaint() is called, but for some reason it does not trigger paintevent(). For example, when I do something like this:

mLCD = new LCDui();
mLCD->drawBlank();

The constructor works just fine for the LCDui class, and draws a black widget, but when the drawBlank() method is called, the widget does not repaint. What am I missing? Thanks for the help! Here is the LCDui class and implementation:


I have narrowed it down to the fact that the widget itself is not updating. If I do mLCD->show();, a new window appears with the correct image, which I can then use normally. However, the widget I want updated is not updated. This widget was promoted to the LCDui class.


lcdui.cpp:

#include "lcdui.h"

#include <QPainter>
#include <QPaintEvent>
#include <QRgb>

LCDui::LCDui(QWidget *parent) : QWidget(parent)
{
    image = new QImage(320,240,QImage::Format_RGB32);
    image->fill(qRgb(0,0,0));
}

LCDui::~LCDui()
{
    delete image;
    image = NULL;
}

void LCDui::paintEvent(QPaintEvent * /*event*/)
{
    QPainter painter(this);
    painter.drawImage(QPoint(0,0),*image);
}

void LCDui::drawBlank()
{
    image->fill(qRgb(255,255,255));
    this->repaint();
}

lcdui.h:

#ifndef LCDUI_H
#define LCDUI_H

#include <QWidget>
#include <QImage>

class LCDui : public QWidget
{
Q_OBJECT
public:
    explicit LCDui(QWidget *parent = 0);
    ~LCDui();
    void drawBlank();

protected:
    virtual void paintEvent(QPaintEvent * /*event*/);

private:
    QImage *image;
};

#endif // LCDUI_H

Solution

  • I forgot to place said functions after I set up the UI.

    ui->setupUi(this);
    ui->LCD_widget->drawBlank();