5

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);
}
pavelkolodin
  • 2,859
  • 3
  • 31
  • 74
  • 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 Nov 21 '12 at 21:38
  • Tilman Vogel i think no - didn't found any direct calling of it. – pavelkolodin Nov 21 '12 at 21:44
  • You can trace your program in debug mode, and check whether it outputs such messages. Provide us more info, plz. – NG_ Nov 21 '12 at 22:31
  • @troyane o... i am not friendly with debugging such code, especially in linux which takes place in my case :) – pavelkolodin Nov 21 '12 at 22:47
  • 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! – NG_ Nov 21 '12 at 23:01
  • @troyane i investigate this problem in linux, Visual Studio is not my option for now :) – pavelkolodin Nov 21 '12 at 23:06
  • its offtop, but I'm talking even about Linux, about QtCreator as IDE – NG_ Nov 21 '12 at 23:07
  • Does this problem arise because of your paint code, or in the QTextEdit::paintEvent( _event ) call? – dowhilefor Nov 23 '12 at 15:19

1 Answers1

8

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

Viv
  • 17,170
  • 4
  • 51
  • 71
  • 2
    Thank you! This helped! The viewport() is a method of QAbstractScrollArea (as i remember), but doesn't exist in ordinary QWidget. And in case you inherit QTextEdit you must remember to use "viewport()", but not "this". It is odd, because you have to remember it... – pavelkolodin Nov 24 '12 at 09:42