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. QPainter does not paint anything
Qt 6.11 is out! See what's new in the release blog

QPainter does not paint anything

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 2.3k Views 1 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.
  • ivictsI Offline
    ivictsI Offline
    ivicts
    wrote on last edited by ivicts
    #1

    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);
    painter->fillRect(multiPlayerDialog->rect(),QColor(105,105,105));
    QPen borderPen(Qt::black);
    borderPen.setWidth(5);
    painter->setPen(borderPen);
    painter->drawRects(boardRects,9); //draw 9 rectangle

    for(int i= 0; i<constTileSquare;i++)
    {
        QString path;
        switch(displayBoard[i])
        {
        case EMPTY:
            path= "";
            break;
        case X:
            path=":/images/X.png";
            break;
        case O:
            path=":/images/O.png";
            break;
        }
    
        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");
    
      multiPlayerDialog->setFixedSize(constTile*100,constTile*100);
    
       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;
    multiPlayerDialog->show();
    

    }

    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?

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

      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.

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

      ivictsI 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        ivictsI Offline
        ivictsI Offline
        ivicts
        wrote on last edited by
        #3

        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?

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

          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.

          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

          • Login

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