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?
update()
, as the documentation clearly says: "This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop [...] Calling update() several times normally results in just one paintEvent() call." It's normally not necessary to manually request repaints, are you asking this out of interest of the Qt behavior, or is this the resulting question caused by a different problem?update
do get optimized, but not the calls torepaint
, but I don't find the source any longer. An I wrong? Regarding your question, this is the resulting question caused by a different problem, which is yet too large to be posted in its current state to a Q&A site. Trying to simplify it, I came to the observation that a given widget in an app is not redrawn when expected, and I was wondering why adding extra calls to force a repaint had no effect.repaint()
multiple times inshowEvent()
may not be effective as you may believe, and you should eventually do a more appropriate test by calling a function that does that, triggered by a QTimer.singleShot launched in theshowEvent()
instead. In any case, this seems more an XY problem. It should almost never be necessary to callrepaint()
, and if it didn't work in your case it may not be related »update()
and absolutely never had to callrepaint()
, let alone doing so in methods such asshowEvent()
/paintEvent()
. This example is going to look a bit too weird for people to be able to figure out a solution to whatever you are trying to do. I concur with @musicamante that it looks like your real problem is something else and most likely will be more relatable to us.