I could not understand the relation between setSceneRect and bounding rectangle.
I need to display a no. of rectangles each placed right to the previous rectangle. Now, as number of rectangles is high therefore i needed a scroll bar, so that i may scroll right to see rest of the rectangles.
My GraphicsView size geometery is
graphicsView
->setGeometry
(QRect(10,
8,
730,
540));
graphicsView->setGeometry(QRect(10, 8, 730, 540));
To copy to clipboard, switch view to plain text mode
so i set my screen rectangle as:
scene->setSceneRect(-360,-266,1000,532);
scene->setSceneRect(-360,-266,1000,532);
To copy to clipboard, switch view to plain text mode
because 730-10 = 720 and 720/2 = 360. Similarly 540-8=532 and 532/2 = 266
I set x2 = 1000 and not 720 because i wanted horizental scroll bars to appear.
Eace time i add a rectangle i pass its size too. I made a tiny class for displaying my rectangles. When i execute my program the rectangles just blink and disappear immediately. The scroll bars do appear but i can't see any of my rectangles. Please help me out. I can't figure out the problem but i am trying.
#include "rectMap.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
static int xLocation = -360;
RectMap::RectMap(int Size)
:color(qrand() % 256, qrand() % 256, qrand() % 256)
{
size = Size;
}
QRectF RectMap
::boundingRect() const {
//...........................................................................................................
I really do not have any idea wat my bounding rectangle should be
//...........................................................................................................
qreal adjust = 0.5;
//return QRectF(-18 - adjust, -22 - adjust,36 + adjust, 60 + adjust);
//return QRectF(-xLocation, -262, size, 523);
return QRectF(-400,
-262,
1000,
532);
}
{
path.addRect(-100, -200, 20, 40);
return path;
}
{
painter->setBrush(color);
painter->drawRect(xLocation,-262,size,523);
updateLocation(size);
}
void RectMap::updateLocation(int size)
{
xLocation = xLocation + size+ 3;
}
#include "rectMap.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
static int xLocation = -360;
RectMap::RectMap(int Size)
:color(qrand() % 256, qrand() % 256, qrand() % 256)
{
size = Size;
}
QRectF RectMap::boundingRect() const
{
//...........................................................................................................
I really do not have any idea wat my bounding rectangle should be
//...........................................................................................................
qreal adjust = 0.5;
//return QRectF(-18 - adjust, -22 - adjust,36 + adjust, 60 + adjust);
//return QRectF(-xLocation, -262, size, 523);
return QRectF(-400,-262,1000,532);
}
QPainterPath RectMap::shape() const
{
QPainterPath path;
path.addRect(-100, -200, 20, 40);
return path;
}
void RectMap::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setBrush(color);
painter->drawRect(xLocation,-262,size,523);
updateLocation(size);
}
void RectMap::updateLocation(int size)
{
xLocation = xLocation + size+ 3;
}
To copy to clipboard, switch view to plain text mode
Bookmarks