Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How can I locate an image in a specific cell of a grid?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved Game Development
12 Posts 3 Posters 1.5k 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.
  • M Offline
    M Offline
    mayyy
    wrote on last edited by
    #3

    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

    jsulmJ 1 Reply Last reply
    0
    • M mayyy

      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

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

      @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
      2
      • M Offline
        M Offline
        mayyy
        wrote on last edited by
        #5

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

        jsulmJ 1 Reply Last reply
        0
        • M mayyy

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

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

          @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
          2
          • M Offline
            M Offline
            mayyy
            wrote on last edited by
            #7

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

            mrjjM 1 Reply Last reply
            0
            • M mayyy

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

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

              @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
              3
              • M Offline
                M Offline
                mayyy
                wrote on last edited by
                #9
                This post is deleted!
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mayyy
                  wrote on last edited by mayyy
                  #10

                  #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

                  jsulmJ 1 Reply Last reply
                  0
                  • M 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

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

                    @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
                    2
                    • M Offline
                      M Offline
                      mayyy
                      wrote on last edited by
                      #12

                      @jsulm thank u , the problem is solved

                      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