5
votes

I have:

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();
};

that gives:

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

Why that could be?

Update

// 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);
}
1
It looks like you are calling QTextEditEnter::paintEvent() explicitly somewhere. Could this be the case? It should only be called directly by Qt from the event loop. - Tilman Vogel
Tilman Vogel i think no - didn't found any direct calling of it. - pavelkolodin
You can trace your program in debug mode, and check whether it outputs such messages. Provide us more info, plz. - tro
@troyane o... i am not friendly with debugging such code, especially in linux which takes place in my case :) - pavelkolodin
In few words. You must change build type (from release) to debug, then add a breakpoint to your code, and run (F5). Use debug controls (step into, step over) to trace your code in realtime. Have a luck! - tro

1 Answers

8
votes

Instead of

QPainter pnt(this);

try

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

viewport() is the paintable area and could be whats causing the issue