Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How can I locate an image in a specific cell of a grid?

    Game Development
    3
    12
    588
    Loading More Posts
    • 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.
    • M
      mayyy last edited by

      Hello all ,
      I'm working on a chess game using Qt ,c++ and I want to locate the positions of pieces on an image to implement the code of the movements after , any help ?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        Hi and welcome to the forums.

        Do you mean like recognizing pieces on a real image of a chessboard?

        Else normally the structure used for the board would keep track on which pieces in which cells.

        1 Reply Last reply Reply Quote 0
        • M
          mayyy last edited by

          Thank you ^^ .
          Ok , u mean that I should implement the code of the board ?
          Actually I'm beginner in Qt and I was trying with this simple one , but I can't display it
          96520498-9159-4861-94b3-7c1b3e720ce9-image.png

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @mayyy last edited by

            @mayyy You should indent your code properly - it is hard to read.
            It should actually be:

            QRect(x*100, y*100, 100, 100);
            

            as you want to draw 100x100 rectangles, right?

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

            1 Reply Last reply Reply Quote 2
            • M
              mayyy last edited by

              ok,the last one is the method display and it is empty .
              yes ,100x100 rectangles.

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @mayyy last edited by jsulm

                @mayyy said in How can I locate an image in a specific cell of a grid?:

                the last one is the method display and it is empty

                What display method?
                One more thing: the painting must be done in paintEvent, not in constructor!
                https://doc.qt.io/qt-5/qwidget.html#paintEvent

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

                1 Reply Last reply Reply Quote 2
                • M
                  mayyy last edited by

                  ok, i will repeat the code , I i thought it is correct coz didn't show any error and thank u

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @mayyy last edited by

                    @mayyy
                    Hi
                    So you could have a list of the cells
                    int boardList [8][8];
                    and put zero in for it being free and else a number indicating
                    what piece is there?
                    You can use an enum to make it more readable
                    enum PieceType {free, king, queen, rook, bishop, knight, pawn };
                    and do
                    boardList[1][2] = pawn;
                    and do
                    boardList[2][2] =boardList[1][2]
                    boardList[1][2] =free;
                    to move it to 2,2 and set 1,2 to free

                    This is very basic. a nicer version could be made.

                    1 Reply Last reply Reply Quote 3
                    • M
                      mayyy last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • M
                        mayyy last edited by mayyy

                        #include "thirdwindow.h"
                        #include "ui_thirdwindow.h"
                        #include <thirdwindow.h>
                        #include <QGraphicsRectItem>
                        #include <QBrush>
                        #include <QPainter>
                        #include <QPaintEvent>
                        #include <QApplication>
                        #include <QGraphicsScene>

                        thirdwindow::thirdwindow(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::thirdwindow)
                        {
                        ui->setupUi(this);
                        }

                        thirdwindow::~thirdwindow()
                        {
                        delete ui;
                        }
                        void thirdwindow::paintEvent(QPaintEvent e){
                        int x=0,y=0;
                        int temp=0;
                        QPainter painter;
                        for(int i=0;i<8;i++)
                        {
                        for(int j=0;j<8;j++)
                        {
                        if(temp==0)
                        {
                        painter.setBrush(Qt::black);
                        temp++;
                        }
                        else
                        {
                        painter.setBrush(Qt::white);
                        temp--;
                        }
                        QRect r (x
                        100, y*100, 100, 100);
                        painter.drawRect(r);
                        x+=100;
                        }
                        x=0;
                        y+=100;
                        if(temp==0)
                        temp=1;
                        else
                        temp=0;
                        }
                        thirdwindow::paintEvent(e);
                        }

                        Hi ,thank you @mrjj for the idea.
                        I used the paintEvent but I got this error
                        QPainter::drawRects: Painter not active
                        QPainter::setBrush: Painter not active

                        jsulm 1 Reply Last reply Reply Quote 0
                        • jsulm
                          jsulm Lifetime Qt Champion @mayyy last edited by

                          @mayyy said in How can I locate an image in a specific cell of a grid?:

                          QPainter::drawRects: Painter not active

                          Because your painter does not know where to paint - set the device:

                          QPainter painter(this); // Or whatever widget you want to paint on
                          

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

                          1 Reply Last reply Reply Quote 2
                          • M
                            mayyy last edited by

                            @jsulm thank u , the problem is solved

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post