Сallrepaint()可见

时间:2019-10-22 13:32:45

标签: c++ qt model-view

有一个视图,其中重新定义了绘制方法。还有一个按钮,当单击时,视图应调用repaint()方法。为方便起见,在此示例中,视图未与模型中的任何数据关联。

#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
#include <QListView>
#include <QScrollArea>
#include <QPushButton>

class View :public QListView {
public:
    void paintEvent(QPaintEvent* event) override
    {
        QListView::paintEvent(event);
        QPainter painter(viewport());
        static int x = 10, y = 10;
        painter.drawPixmap(x, y, QPixmap("cheker.png"));
        x += 10;
        y += 10;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    View view;

    QPushButton btn("btn");
    btn.move(100, 300);
    QObject::connect(&btn, SIGNAL(clicked()), &view, SLOT(repaint()));

    btn.show();
    view.show();
    return a.exec();
}

我发现,第一次单击按钮后,repaint()仅被调用一次。第2、3、4等次按按钮repaint()不会被调用。为什么?

0 个答案:

没有答案