Important: Please read the Qt Code of Conduct -

QPainter does not work though I put them in the paintEvent

  • Canvas is a derived class of QLabel

    void Canvas::paintEvent(QPaintEvent*){
        QPainter painter(this);
        painter.setFont(QFont("MaestroTimes", 10));
        std::cout << height() << " " << width() << std::endl;
        painter.drawLine(0, static_cast<int>(height()/2), width()-1, static_cast<int>(height()/2));
    QLabel* ScoreMaker::makeScore(const QQueue<CommandTimer*>* timers, int interval, int lastInterval, int height){
        int width = 30;
        Canvas* score = new Canvas();
        score->setFixedWidth(static_cast<int>((width + lastInterval) / interval));
        return score;

    Also, update() and repaint() does not trigger paintEvent so I had to use

    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

    Those errors occur even thought I put them in the paintEvent

  • Lifetime Qt Champion


    Don’t call paintEvent by hand, that’s for the system to do. It will be called automatically when needed. For example, when your widget is shown, when you move it, etc.

  • It seems that I have to set parent of widget before calling repaint(). Otherwise, paintEvent is not called.

  • Lifetime Qt Champion

    That’s not mandatory, however you need to call show on the widget depending on how you use it.

Log in to reply