我有一个QWidget
派生类。在构造函数中我说:
setPalette(QPalette(QColor(250,250,200)));
setAutoFillBackground(true);
然后在我的小部件paintEvent()
中我说:
QPainter painter(this);
painter.drawRect(1,2,3,4);
还有一个updateNow()
广告位...只需拨打update()
。如何在更新调用后确保我的调色板不会被删除?
答案 0 :(得分:4)
我对以下内容没有任何问题:
#include <QApplication>
#include <QWidget>
#include <QPalette>
#include <QPaintEvent>
#include <QPainter>
class Test : public QWidget
{
public:
Test()
{
setPalette(QPalette(QColor(250, 250, 200)));
setAutoFillBackground(true);
}
protected:
virtual void paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.drawRect(10, 20, 30, 40);
}
virtual void mousePressEvent(QMouseEvent*)
{
update();
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Test myTest;
myTest.show();
return app.exec();
}
矩形绘制,点击后停留,触发更新。你在看什么?