Bug QPainter crashes

  • Hello. I am new to Qt. I was learning how to use QPainter and I am stuck. I was fighting this bug for few days but no result. What I am trying to do is to draw a snake on a board by QPainter. But application crashes after some time. I suspect an infinite loop somewhere but snake doesn't get drawn whereas board does. Here is the code I think is relevant:

    MainWindow::MainWindow(QWidget* parent) :
        Board* board = new Board(this);
        Snake* snake = new Snake(board);
    void Snake::paintEvent(QPaintEvent* event)
        QPainter painter(board);
        QPen pen;
        QBrush brush;
        for(int i = 0; i < snake.size(); i++)
            if(i = 0)
                pen.setWidth(board -> SNAKE_HEAD_BORDER_SIZE);
                pen.setColor(board -> SNAKE_HEAD_BORDER_COLOR);
                brush.setColor(board -> SNAKE_HEAD_COLOR);
                pen.setWidth(board -> SNAKE_BORDER_SIZE);
                pen.setColor(board -> SNAKE_BORDER_COLOR);
                brush.setColor(board -> SNAKE_COLOR);
            painter.drawEllipse(snake[i].x, snake[i].y,
                board -> DOT_SIZE, board -> DOT_SIZE);
            qDebug() << "paintEvent called";
            painter.drawLine(20, 160, 250, 160);

    QPaintEvent of a snake does get called though. It just doesn't draw.

    [Added code tags ~kshegunov]

  • Qt Champions 2016

    @Sproza said in Bug QPainter crashes:

    snake[i].x, snake[i].y

    Have you checked the values of x,y when it dont draw?
    Maybe its just outside widget area.

    Also, what is snake ?
    It looks like one class but in paint you seems to use
    as a array ??
    but you create it like
    Snake* snake = new Snake(board);

    But in any case, its very hard to see the bug from code.
    You should use the debugger and single step the code until you see what line it crashes
    in or some value that is looking strange.

  • @Sproza said in Bug QPainter crashes:

    MainWindow::MainWindow(QWidget* parent) :
    Board* board = new Board(this);
    Snake* snake = new

    You are creating some local pointer variables like
    Board* board = new Board(this);
    Snake* snake = new Snake(board);

    Inside the paint event of snake you r trying to pass the board to Qpainter argument. At the same to board parent to snake object. Surely all this object relation ship must causing some issue. It is better to break the problem into smaller set & see if the issue exist.

    I prefer you try just a Snake Class. Try to create object and see if this works. Then move the the board.

  • Qt Champions 2016

    Good catch. \o/

  • Snake is a class and snake is an member vector. In this case snake is also an instance. I made the whole code available at: https://github.com/Sproza/Snake. Board itself works fine. NOTE: drawLine() in paintEvent of both Snake and Board class was only for debugging purposes forgot to delete it sorry.

  • Board class alone works fine. Still no clue about Snake class though :/

  • Lifetime Qt Champion

    On a side note, modifying your pen and brush objects after setting them on painter won't modify what painter draws. You should first configure them and once that done set them on painter.

  • thanks @SGaist! it should be obvious, a silly mistake on my part. Any ideas about the problem though??

  • Lifetime Qt Champion

    What's the size of your snake variable ? Are you sure the coordinates are valid ?

  • Yes, coords are valid (250x and 250y whereas border is 500x500). Size of snake vector is 2.
    Whole code is available at: https://github.com/Sproza/Snake.git
    Just to make it clear because many find it confusing: Snake is a class with snake member vector and I named an instance of Snake "snake" in mainwindow constructor.

  • Please? Anyone?

  • Lifetime Qt Champion

    Can you provide a more complete code sample ? We can only do guesses here since there are a lot of unknowns from your side.

  • Qt Champions 2016

    The code on git do not really compile out of the box.
    Anyway, i saw this

    for(int i = snake.size(); i >= 0; i--)

    if snake.size() is 0 , wont that crash in snake[0].x ?
    as you say >= ?

  • Moderators

    @mrjj @Sproza Yes, it should be

    for(int i = snake.size() - 1; i >= 0; i--)

  • Yes, in that case it will crash, but snake.size() will never be 0 unless board -> STARTING_LENGTH will be 0. In Snake constructor program pushes additional dot to vector STARTING_LENGTH times.
    But in reality there wont be way to change STARTING_LENGTH to 0 via menu.
    So I do no think that's the reason. Just to be sure I tried snake.size() - 1 (although i have no idea why it would work) and it is not the solution.

  • Qt Champions 2016


    Ok, since you can run it then why not just find the crash with the debugger ?
    When it crash the call stack will be shown and you can easy see where the crash happened.

Log in to reply

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