PDA

View Full Version : QGraphicsView repainting item problem with ScrollBar



Grade
17th July 2013, 22:55
Hi everyone.
I'm currently making a Map Editor with Qt, and I have a problem I don't understand.
I have a QGraphicsView which draws my tileset and I can select one/multiple tiles by clicking them (If I hold the clic I can select several tiles at one time). It works fine but here's my problem: My QGraphicsView has a verticalScrollBar, and when I scroll over/under the cursor, the cursor gets cut (not everytime though), but it's always the bottom part of the cursor that gets cut.

Here the code of my cursor (it's a custom QGraphicsItem) :

cursoritem.h

#ifndef CURSORITEM_H
#define CURSORITEM_H

#include <QGraphicsItem>

class Fenetre;

class CursorItem : public QGraphicsItem
{
public:
CursorItem(Fenetre* parent=0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget);
void setX(int i);
void setY(int i);
void setW(int i);
void setH(int i);

private:
Fenetre* fenetre;
int x;
int y;
int w;
int h;

};

#endif // CURSORITEM_H


cursoritem.cpp

#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QMessageBox>
#include "cursoritem.h"
#include "fenetre.h"

CursorItem::CursorItem(Fenetre *parent) :
QGraphicsItem(), fenetre(parent), x(0),y(0),w(1),h(1)
{
}

QRectF CursorItem::boundingRect() const
{
return QRectF(0, 0,
w*32, h*32);
}

void CursorItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->drawRect(0,0,w*32,h*32);
}

void CursorItem::setX(int i)
{ x=i; }

void CursorItem::setY(int i)
{ y=i; }

void CursorItem::setW(int i)
{ w=i; }

void CursorItem::setH(int i)
{ h=i; }


If you need anymore details just ask. I'd really appreciate if someone could help me.
Thanks!

Grade
18th July 2013, 17:05
Fixed.
I just needed to use QGraphicsItem::prepareGeometryChange() to tell that the cursor's BoundingRect has changed.