Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble getting a derived QWidget to paint itself
    primarykey
    data
    text
    <p>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:</p> <pre><code>#ifndef GRADIENT_H #define GRADIENT_H #include &lt;QBrush&gt; #include &lt;QPen&gt; #include &lt;QPixmap&gt; #include &lt;QWidget&gt; 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 </code></pre> <p>Then, the .cpp:</p> <pre><code>#include &lt;QtGui&gt; #include &lt;QPen&gt; #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 &lt; height(); ++y) { painter.save(); //painter.setPen(); &lt;---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)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload