mercredi 30 septembre 2020

draw shifts in QGraphicsView

I have a weird shift when I draw in a QGraphicsView object,

enter image description here

The black square with inner red squares should be drawn in the upper left corner.

The .ui file :

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>600</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Wator</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QScrollArea" name="scrollArea">
      <property name="widgetResizable">
       <bool>true</bool>
      </property>
      <widget class="QWidget" name="scrollAreaWidgetContents">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>580</width>
         <height>536</height>
        </rect>
       </property>
       <layout class="QGridLayout" name="gridLayout_2">
        <item row="0" column="0">
         <widget class="QGraphicsView" name="graphicsView"/>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>600</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

How .ui looks like in designer mode : enter image description here

And finally, the code in MainWindow that I draw from :

#include "Mainwindow.hpp"
#include "ui_mainwindow.h"
#include "Environment.hpp"
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    scene = new QGraphicsScene();
    ui->graphicsView->setScene(scene);
    scene->setBackgroundBrush(QColor(0, 0, 255));

    grid_pen = QPen(Qt::black);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(drawEnvironment()));
    timer->start(1000);
    tick = 0;

    initializeEnvironment();

    QRect rcontent = ui->graphicsView->contentsRect();
    ui->graphicsView->setSceneRect(0, 0, rcontent.width(), rcontent.height());
    ui->graphicsView->setFixedWidth(50 * environment->getWidth());
    ui->graphicsView->setFixedHeight(50 * environment->getHeight());
    ui->graphicsView->setInteractive(false);
    drawEnvironment();
}

void MainWindow::initializeEnvironment(){
    this->environment = new Environment(10, 10);

    std::vector<Agent*> agents;
    for(uint32_t i = 0; i < 20; i++){
        agents.push_back(new Agent());
    }

    this->environment->placeAgents(agents);
}

void MainWindow::drawEnvironment(){
    scene->clear();
    scene->addRect(0, 0, ui->graphicsView->width(), ui->graphicsView->height(), grid_pen, ocean_brush);
    uint16_t cell_width = 50;
    uint16_t cell_height = 50;
    std::vector<Agent*> agents = environment->getAgents();
    for(uint16_t i = 0; i < agents.size(); i++){
        uint16_t x = agents[i]->getX();
        uint16_t y = agents[i]->getY();
        QBrush agent_brush(agents[i]->getColor());
        scene->addRect(x * cell_width, y * cell_height,cell_width, cell_height, grid_pen, agent_brush);
        std::cout << "(" << x << ";" << y << ")" << std::endl;
    }
    tick++;
}

MainWindow::~MainWindow()
{
    delete timer;
    delete environment;
    delete scene;
    delete ui;
}

I verified the coordinates of my Agents, and they are indeed between 0 and 10, so the squares should be drawn in [0; 500]px. What could cause such issue ?

Aucun commentaire:

Enregistrer un commentaire