I am getting this error "QPainter::setPen: Painter not active"



  • @QPainter myPainter(ui->graphicsView);
    myPainter.setPen(QPen(QBrush("#535353"), 0.5));

    myPainter.setRenderHint(QPainter::Antialiasing);
    
    int h = height();
    int w = width();
    
    myPainter.translate(QPoint(w/2, h/2));
    
    for (qreal rot=0; rot < 360.0; rot+=5.0 )
    {
        myPainter.drawEllipse(-125, -40, 250, 80);
        myPainter.rotate(5.0);
    }@
    

    error:
    @QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::setPen: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints
    QPainter::translate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active
    QPainter::rotate: Painter not active@



  • You should subclass QGraphicsView, then put these code to its paintEvent();



  • However, I don't know why you want to draw onto QGraphicsView, instead of QGraphicsScene.



  • Message is clear.

    If you want to Use QPainter to paint on any QPaintDevice you should reimplement
    @virtual void QWidget::paintEvent ( QPaintEvent * event )@
    In your case you need to subclass the QGraphicsView and reimplement the function, or subclass this widget, depending what you need.
    Inside that function put your code.
    you can also use the QPainter::begin function.

    To triger painting call the update() or redraw() in place where you need to trigger paining manually.



  • [quote author="gmaro" date="1333663206"]
    If you want to Use QPainter to paint on any QPaintDevice you should reimplement
    [/quote]

    Not any, just QWidget and all its subClasses. Other QPaintDevice, such as QImage, QPixmap, etc doesn't have relationship with paintEvent().



  • Yes I know that, I was refering to the current case only, and it's clear it's about the widgets.

    Btw. It would be hard to reimplement the QImage or QPixmap's paintEvent function :)



  • Thanks for your replies.

    Actually I am implementing a crop function. I want to draw a rectangle on an image displayed in qgraphicsview widget. the rectangle should be resizable on its ends using mouse. Once the user finalises the part of image, the program would use copy function of QImage to extract the cropped part.
    So, can you plz guide me with creating a resizable rectangle. If I reimplement the painter function of my mainwindow class, the rectangle gets displayed at the beginning. It should appear only when crop is invoked.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.