QWidget::paintEngine: Should no longer be called



  • I saw topics of people who has the same error. I understand that all using of QPainter must happened in the paintEvent. I done this, but problem still exist.
    This is the code of function:

    void GraphicsView::painEvent(QPaintEvent *event)
    {
        QPainter  painter(viewport());
    
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    }
    

    In short, I create class that inherited from QGraphicsView. I rewrite some functions, but I do not think that problem in them. There is a full code of my program:

    GraphicsView.h

    #include <QGraphicsView>
    #include <QEvent>
    #include <QPainter>
    #include <QPaintEvent>
    
    class GraphicsView : public QGraphicsView
    {
        Q_OBJECT
    
        using  QObject :: event;
        using  QGraphicsView :: paintEvent;
    
    public:
        GraphicsView();
        virtual ~GraphicsView();
    
    
        bool eventFilter(QObject *object, QEvent *event);
        virtual void painEvent(QPaintEvent * event);
    };
    
    

    GraphicsView.cpp

    #include "graphicsview.h"
    
    GraphicsView::GraphicsView():QGraphicsView ()
    {
        this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }
    
    
    GraphicsView::~GraphicsView()
    {
    
    }
    
    
    // I changed there the event when wheel is moving
    bool GraphicsView::eventFilter(QObject *object, QEvent *event)
    {
        if(event->type() == QEvent::Wheel)
        {
            return  true;
        }
        return true;
    }
    
    void GraphicsView::painEvent(QPaintEvent *event)
    {
        QPainter  painter(viewport());
    
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    }
    
    

    main.cpp

    #include "graphicsview.h"
    #include <QApplication>
    #include <QGraphicsScene>
    #include <QGraphicsView>
    
    #include <QMouseEvent>
    #include <QUrl>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    
        QRect r;
        r.setX(0);
        r.setY(0);
        r.setWidth(1920);
        r.setHeight(1080);
    
        QGraphicsScene  scene(r);
    
        GraphicsView  view;
        view.setScene(&scene);
    
        
        view.update();
        view.showFullScreen();
    
        return a.exec();
    }
    
    

    This is the errors, may be it will help:

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active
    QPainter::setFont: Painter not active
    

    Can somebody help me? Thanks in advance.


  • Qt Champions 2018

    I don't see any debug / warning output on Linux with qt5.12/13



  • @Staslend

    I understand that all using of QPainter must happened in the paintEvent

    void GraphicsView::painEvent(QPaintEvent *event)

    Look at your typing/spelling!? (Or is that not relevant here? I assume you're trying to override?)


  • Lifetime Qt Champion

    Hi,

    When re-implementing a function like that, use the override keyword. It will trigger a warning during build if no matching method can be found in one of the base class.



  • @SGaist thank you for help, I have no errors not with QPainter. But I can't use override word with paintEvent, I have this error: 'paintEvent' marked 'override' but does not override any member function. With eventFilter it working.


  • Qt Champions 2018

    @JonB said in QWidget::paintEngine: Should no longer be called:

    painEvent

    The override keyword works, when you've the function name right.



  • @Staslend
    http://doc.qt.io/qt-5/qgraphicsview.html#paintEvent

    void QGraphicsView::paintEvent(QPaintEvent *event)    [override virtual protected] 
    


  • @Christian-Ehrlicher said in QWidget::paintEngine: Should no longer be called:

    @JonB said in QWidget::paintEngine: Should no longer be called:

    painEvent

    The override keyword works, when you've the function name right.

    But that's what I've been saying to the OP...! I quoted what he has written.... He is now saying he cannot use paintEvent spelt correctly with override, so I guess he needs to show us what actually tried?



  • Thanks for everyone! I am sorry for this mistake. The QtCreator write painEvent using Italic so I thought that the name of function was written right. Everything is working now.



  • @Staslend if your issue is solved, please don't forget to mark your post as such! Thanks.