Given the following main window implementation:
#include <QApplication>
#include "MainWindow.h"
namespace ui {
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
child(this) {
this->resize(425, 450);
this->child.move(10, 10);
this->child.resize(400, 400);
}
MainWindow::~MainWindow() {}
void MainWindow::showEvent(QShowEvent* event) {
QWidget::showEvent(event);
qDebug() << "Updating.";
this->child.update();
this->child.update();
this->child.update();
this->child.update();
this->child.repaint();
this->child.repaint();
this->child.repaint();
this->child.repaint();
QApplication::processEvents();
}
} // namespace ui
and the widget which looks like this:
#include <QPainter>
#include "Widget.h"
namespace ui {
Widget::Widget(QWidget *parent) :
QWidget(parent) { }
void Widget::paintEvent(QPaintEvent *event) {
qDebug() << "paintEvent called.";
QWidget::paintEvent(event);
}
} // namespace ui
I would expect to see at least four (and possibly more than eight) “paintEvent called.” messages in the console. However, the console just shows:
Updating.
paintEvent called.
paintEvent called.
The output is exactly the same if I remove all the this->child.update()
and this->child.repaint()
calls in MainWindow.cpp
.
If update()
and repaint()
don’t actually update or repaint a widget, what should I do instead in order to force a redraw of a widget?