Почему QPainter может отказываться работать внутри paintEvent?

Я имею:

class QTextEditEnter : public QTextEdit
{
Q_OBJECT
public:
QTextEditEnter( QWidget *_parent ) : QTextEdit(_parent)
{
this -> setFrameStyle( QFrame::Sunken ); // Sunken!
}

protected:
virtual void keyPressEvent(QKeyEvent * event);
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt(this);
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width(), height());
}

signals:
void signalPressEnter();
};

это дает:

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setPen: Painter not active
QPainter::drawRects: Painter not active

Почему это может быть?

Обновить

// QPainter( this )
// QTextEdit::paintEvent at the begining of custom PaintEvent
// RESULT: "QPainter::begin: Widget painting can only begin as a result of a paintEvent" ...
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt( this );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
}

// QPainter ( viewport() )
// QTextEdit::paintEvent at the begining of custom PaintEvent
// RESULT: works.
virtual void paintEvent(QPaintEvent *_event)
{
QTextEdit::paintEvent( _event );
QPainter pnt( viewport() );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
pnt.drawRect( 0, 0, width()-1, height()-1);
}

// *** BONUS ***
// QPainter( viewport() ) or QPainter ( this )
// QTextEdit::paintEvent after QPainter() constructor.
// RESULT: Segmentation fault.
virtual void paintEvent(QPaintEvent *_event)
{
QPainter pnt( viewport() );
pnt.setPen( QColor( 0, 0, 0, 0xff ));
QTextEdit::paintEvent( _event ); // WRONG PLACE!!!
pnt.drawRect( 0, 0, width()-1, height()-1);
}

4

Решение

Вместо

QPainter pnt(this);

пытаться

QPainter pnt(viewport());
pnt.setPen(QColor( 0, 0, 0, 0xff ));
pnt.drawRect(viewport()->rect());

viewport () — это окрашиваемая область, которая может быть причиной проблемы

7

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]