How can I locate an image in a specific cell of a grid?
-
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.
-
@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 -
@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 freeThis is very basic. a nicer version could be made.
-
#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 (x100, 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 -
@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