Important: Please read the Qt Code of Conduct -

QPainter does not paint anything

  • Hi, I am new to QT.
    Here , I am trying to build tictactoe, so I have to draw rectangles in tictactoe. First , I create dialogs that ask the game settings. Then, after that I create dialog that will be the tictactoe game itself .

    void Board::paintEvent(QPaintEvent *e)
    painter = new QPainter(multiPlayerDialog);
    QPen borderPen(Qt::black);
    painter->drawRects(boardRects,9); //draw 9 rectangle

    for(int i= 0; i<constTileSquare;i++)
        QString path;
        case EMPTY:
            path= "";
        case X:
        case O:
        painter-> drawPixmap(boardRects[i].x()+10,   boardRects[i].y()+10,boardRects[i].width()-20,  boardRects[i].height()-20,QPixmap(path));


    void Board::multiPlayer()


     delete dialogGetPlayerName;
      multiPlayerDialog = new QDialog(this);
      multiPlayerDialog->setWindowTitle("Multi Player Tic Tac Toe");
       QSize size(100,100);
        boardRects[0] = QRect(QPoint(0,0),size);
        boardRects[1] = QRect(QPoint(0,100),size);
        boardRects[2] = QRect(QPoint(0,200),size);
        boardRects[3] = QRect(QPoint(100,0),size);
        boardRects[4] = QRect(QPoint(100,100),size);
        boardRects[5] = QRect(QPoint(100,200),size);
        boardRects[6] = QRect(QPoint(200,0),size);
        boardRects[7] = QRect(QPoint(200,100),size);
        boardRects[8] = QRect(QPoint(200,200),size);
    player = X;


    However, it does not show any rectangle. It just shows a new blank window. What's wrong?
    How do you do painting in another dialog besides the main dialog? How to tell the QPaintEvent to work only on this multiplayerPlayerDialog?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Short answer: you don't.

    Longer answer: each widget is responsible for its own painting. So you should configure you custom dialog with whatever parameters are needed and let it handle the painting.

  • Hi,
    Thank you for your reply
    it actually work if I change multiPlayerDialog -> show() to this-> show() .
    Is it what do you mean? So, it is because I created the BoardRects at the class not at the dialog itself?

  • Lifetime Qt Champion

    I knew I missed something :D

    You are currently doing your custom painting in Board not in multiplayerDialog which is just an empty QDialog. That's why calling show on Board shows you what you want.

Log in to reply