Question

I have a problem: calling QTableWidget::update does not result in QTableWidget::paintEvent.

Brief description: - QTableWidgetEx - a class derived from QTableWidget, with paintEvent overriden. - Some code creating QTableWidgetEx and two menu items 'call_update' and 'check_paint_cnt'

Testing sequence

  • Click 'check_paint_cnt' - status bar shows "paint_cnt = 1" (so paintEvent is called)
  • Click 'call_update'
  • Click 'check_paint_cnt' again - status bar shows "paint_cnt = 1", but should be 2... (no call to paintEvent happen)
  • Resizing the window does increase paint_cnt, so the function is overriden successefully and is called sometimes, but not with update or repaint.

(Small test app - below. The test project as an empty Qt project, created by wizard (Qt 5.2): http://s000.tinyupload.com/index.php?file_id=57865294773039719910 - full source code.)

I tested 'repaint' instead of 'update' - it gives the same result.

Question: Documentation says QTableWidget::update should trigger QTableWidget::paintEvent, but it doesn't What am I doing wrong?

I also tested, and have the same result (not working):

  • update(rect)
  • repaint, repaint(rect)
  • updateGeometry()
  • w->resize(w->width(), w->height());

Good workaround is:

inline void wa_widget_update(QWidget* w)
    {
    if(auto a = dynamic_cast<QAbstractScrollArea*>(w))
            a->viewport()->update();
    else
            w->update();
    };

instead of w->update() use wa_widget_update(w). A bit ugly but works. Reported this bug to qt project: Link to bugtracker

Full sources:

#--------------------------------------------------
# Qt_Update_Test.pro contents
#--------------------------------------------------
QT             += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET         = Qt_Update_Test
TEMPLATE       = app
SOURCES        += main.cpp
HEADERS        += main.h

//--------------------------------------------------
// main.h contents
//--------------------------------------------------

#ifndef MAIN_H
#define MAIN_H

#include <QApplication>
#include <QMainWindow>
#include <QTableWidget>
#include <QPaintEvent>
#include <QStatusBar>
#include <QMenuBar>

extern int paints_cnt;    // Global variable - paintEvent calls counter

class QTableWidgetEx : public QTableWidget
    {
    Q_OBJECT
    public:
        inline explicit QTableWidgetEx(QWidget *parent = 0) : QTableWidget(parent) {};
    protected:
        virtual void paintEvent(QPaintEvent* e) override;
    };


class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
        QTableWidgetEx*        table_widget_ex;
        QMenuBar*              menuBar;
        QStatusBar*            statusBar;

        explicit MainWindow(QWidget *parent = 0);
        inline ~MainWindow(){};
    protected slots:
        void on_call_update();
        void on_check_paint_cnt();
    };

#endif

//--------------------------------------------------
// main.cpp contents
//--------------------------------------------------

#include "main.h"
int paints_cnt = 0;    // Global variable - paintEvent calls counter

void QTableWidgetEx::paintEvent(QPaintEvent* e)
    {
    ++paints_cnt;
    QTableWidget::paintEvent(e);
    }

void MainWindow::on_call_update()
    {
    table_widget_ex->update();
    };

void MainWindow::on_check_paint_cnt()
    {
    statusBar->showMessage("paints_cnt = " + QString::number(paints_cnt));
    };

// Below - Layout code, signal-slots, entry point, etc...
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
    resize(400, 300);

    menuBar = new QMenuBar(this);
    setMenuBar(menuBar);

    table_widget_ex = new QTableWidgetEx(this);
    table_widget_ex->setUpdatesEnabled(true);
    setCentralWidget(table_widget_ex);

    statusBar = new QStatusBar(this);
    setStatusBar(statusBar);

    auto call_update = new QAction("call_update", this);
    connect(call_update, SIGNAL(triggered()), this, SLOT(on_call_update()));
    menuBar->addAction(call_update);

    auto check_paint_cnt = new QAction("check_paint_cnt", this);
    connect(check_paint_cnt, SIGNAL(triggered()), this, SLOT(on_check_paint_cnt()));
    menuBar->addAction(check_paint_cnt);
    };

int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();    
    return a.exec();
    };   
Was it helpful?

Solution

QTableWidget is derived from QAbstractScrollArea and uses a viewport widget. If you call the update() method of the viewport widget, the paintEvent will get triggered:

void MainWindow::on_call_update()
{
    table_widget_ex->viewport()->update();
}

More info also at: update() or repaint() fails to trigger paintEvent()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top