Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bug QPainter crashes

Bug QPainter crashes

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 5.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sproza
    wrote on last edited by kshegunov
    #1

    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) :
        QMainWindow(parent)
    {
        Board* board = new Board(this);
        Snake* snake = new Snake(board);
    }
    
    void Snake::paintEvent(QPaintEvent* event)
    {
        QPainter painter(board);
        QPen pen;
        painter.setPen(pen);
        QBrush brush;
        painter.setBrush(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);
            }
            else
            {
                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]

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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 ??
      snake.size();
      snake[i].x
      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.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by dheerendra
        #3

        @Sproza said in Bug QPainter crashes:

        MainWindow::MainWindow(QWidget* parent) :
        QMainWindow(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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        mrjjM 1 Reply Last reply
        6
        • dheerendraD dheerendra

          @Sproza said in Bug QPainter crashes:

          MainWindow::MainWindow(QWidget* parent) :
          QMainWindow(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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @dheerendra
          Good catch. \o/

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sproza
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sproza
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sproza
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Sproza
                      wrote on last edited by Sproza
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sproza
                        wrote on last edited by
                        #11

                        Please? Anyone?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Hi
                            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 >= ?

                            jsulmJ 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              Hi
                              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 >= ?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @mrjj @Sproza Yes, it should be

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

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              2
                              • S Offline
                                S Offline
                                Sproza
                                wrote on last edited by
                                #15

                                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.

                                mrjjM 1 Reply Last reply
                                0
                                • S Sproza

                                  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.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @Sproza

                                  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.

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved