0

Bellow you can see minimal example code to demonstrate problem. If you run it and give focus to QLineEdit, you get output every second: paintEvent, paintEvent and so on. I can not understand why MyW::paintEvent called on every cursor blinking in child widget? As you see I do not configure QLineEdit, by default on my linux box it have no transparent elements, but still for some reason cursor cause all widgets to redraw their content?

#include <QApplication>
#include <QWidget>
#include <QPaintEvent>
#include <QPainter>
#include <QLineEdit>

class MyW final : public QWidget {
public:
    MyW() {
        //setAutoFillBackground(false);
    }
    void paintEvent(QPaintEvent *e) {       
        e->accept();
        qDebug("paintEvent");
        QPainter painter{this};
        painter.fillRect(rect(), Qt::green);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MyW w;
    w.resize(600, 600);
    w.show();
    auto le = new QLineEdit{&w};
    le->show();
    return app.exec();
}
user1244932
  • 7,352
  • 5
  • 46
  • 103
  • 1
    many times they are repainted as they expect to interact with the mouse, for example they are processing the hover event, and so other types of events, is a normal behavior. – eyllanesc Mar 23 '18 at 09:13
  • I think this is standard behavior by `paintEvent`. It'd make no sense for a `paintEvent` not to be issued whenever the widget needs to be painted. May be you can use `update()` to handle widget updates. Have a look at [how-can-i-make-paintevent-triggered-only-when-update-is-called](https://stackoverflow.com/questions/18777535/how-can-i-make-paintevent-triggered-only-when-update-is-called). – Sumit Jha Mar 23 '18 at 09:30
  • @SumitJha `not to be issued whenever the widget needs to be painted` but what the to redraw `MyW` if cursor blink in widgets on top of it, this is very simple optmization, and this is question why Qt not do such kind of optimization. – user1244932 Mar 23 '18 at 14:49
  • @eyllanesc Yes mouse moving is also amazing reason to redraw. I read that since Qt 5 it uses double buffering by default, why not just take pixmap from buffer cache, why issue qpaintevent instead. – user1244932 Mar 23 '18 at 14:52

0 Answers0