Reputation: 253
I have made my own simple widget by deriving from QWidget, and am trying to get it to paint a gradient inside its paintEvent(). I set a breakpoint inside paintEvent, but it never gets called, even when I resize the window etc. The widget is a promoted QWidget from within the designer. Here is my object code - I am not actually reading the gradient colors yet, just trying to get the paintEvent() to fire first hehe. First, the header file:
#ifndef GRADIENT_H
#define GRADIENT_H
#include <QBrush>
#include <QPen>
#include <QPixmap>
#include <QWidget>
class Gradient : public QWidget
{
Q_OBJECT
public:
Gradient(QWidget *parent = 0);
QSize minimumSizeHint() const;
QSize sizeHint() const;
public slots:
protected:
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
private:
};
#endif // GRADIENT_H
Then, the .cpp:
#include <QtGui>
#include <QPen>
#include "gradient.h"
Gradient::Gradient(QWidget *parent)
: QWidget(parent)
{
//setBackgroundRole(QPalette::Base);
//setAutoFillBackground(true);
}
QSize Gradient::minimumSizeHint() const
{
return QSize(20, 256);
}
QSize Gradient::sizeHint() const
{
return QSize(20, 512);
}
void Gradient::resizeEvent(QResizeEvent * /* event */)
{
//update();
repaint();
}
void Gradient::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
painter.setPen(QPen(Qt::blue,1));
// painter.setBrush(?);
for (int y = 0; y < height(); ++y)
{
painter.save();
//painter.setPen(); <---here I plan to set different colors from a dynamic table
painter.translate(0, y);
painter.drawLine(0, 0, width(), 0);
painter.restore();
}
painter.drawRect(QRect(0, 0, width() - 1, height() - 1));
}
Upvotes: 2
Views: 1202
Reputation: 22272
There is no need to call update()
or repaint()
inside the resizeEvent()
. But that shouldn't break anything either. There must be something wrong with the promoted stuff because that paintEvent()
override is correct. I pasted it into a QWidget subclass on my end and it works. Perhaps a clean all and rebuild?
Upvotes: 1