[Qt-interest] setSceneRect vanished my items from GraphicsView :(

Usman Ajmal uzmanajmal at gmail.com
Sun Jul 12 13:20:11 CEST 2009


Thanks Enrico but you may see that some of your rectangles are not showing
up fully, meaning you can't see some of the rectangles' bottom and right
parts. Also, the left and top part of your GraphicsView are white. Isn't it
abnormal?

I think there is some thing wrong with my bounding rectangle but i couldn't
understand the relation between bounding rectangle and setSceneRectangle
even by viewing the Mice Collider examply of Trolltech.

My code that may have some problem is as under:

void MainWindow::drawMap()
    {
        QGraphicsScene *scene = new QGraphicsScene;
        scene->setSceneRect(-360,-262,1000,532);            //  <----- This
line may have problem
        scene->setItemIndexMethod(QGraphicsScene::NoIndex);
        for (int i = 0; i < 50; ++i) {
        RectMap *rectangle = new RectMap(50); //50 is the width of
rectangles

        scene->addItem(rectangle);
        }
        graphicsView->setRenderHint(QPainter::Antialiasing);

//graphicsView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
        graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
        graphicsView->setScene(scene);
        graphicsView->show();
    }

RectMap is custom class for making rectangles. RecMap class is:

 #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
{
     qreal adjust = 0.5;
     //return QRectF(-18 - adjust, -22 - adjust,36 + adjust, 60 + adjust);
//  <---- This line may have problem
  return QRectF(-xLocation, -262, size, 523);
}

 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;
}

Thanks a lot...

--
Usman
I hope i complete my final degree project :)
On Sun, Jul 12, 2009 at 1:27 PM, Enrico Ros <enrico.qt at email.it> wrote:

> On Sunday 12 July 2009 08:32:55 Usman Ajmal wrote:
> > Hi All,
> >
> > I having a problem with a GraphicsView widget. I am trying to display 50
> > rectangles in the Graphics View widget but size of the rectangles are
> such
> > that i need horizental scroll bars to view all the rectangles.
> >
> > My GraphicsView size is  (height, width) = (710, 530). When i set my
> scene
> > rectangle to say 'setSceneRect(-20,-20,10,10)', i see some of my drawn
> > rectangles. In order to view all rectangles i set the scene rectangle to
> > (-360,-256,1000,530). I set 1000 so that i may get a horizental scroll
> bar.
> >
> > *Question:*
> >
> > I now, got the horizental scroll bar but i don't see 'any' of my
> rectangles
> > at all. When i executed my program my rectangle just blinked and
> > disappeared, leaving behind a white graphiceView plate of confusion.
> >
> > Any idea what i am missing here?
>
> I tried this test code to simulate your behavior:
> ---------------------------
> #include <QtGui>
>
> int main(int argc, char *argv[])
> {
>    QApplication a(argc, argv);
>
>    QGraphicsView * gw = new QGraphicsView;
>    gw->setFixedSize(710, 530);
>    gw->show();
>
>    QGraphicsScene * scene = new QGraphicsScene;
>    gw->setScene(scene);
>    gw->setSceneRect(-360,-256,1000,530);
>
>    for (int i = 0; i < 50; i++)
>        scene->addRect(qrand() % 200, qrand() % 200, 10 + qrand() % 500, 10
> +
> qrand() % 500, QColor(qrand()%255, qrand()%255, qrand()%255));
>
>    return a.exec();
> }
> ---------------------------
> and this works as expected! Analyze this and see if you can find something
> interesting. Also try to see if your items are deleted (place a breakpoint
> on
> destructor, for example) or if the scene is compromised by any later calls.
>
> Enrico Ros
>
>
>
>
>
>  --
>  Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP
> autenticato? GRATIS solo con Email.it http://www.email.it/f
>
>  Sponsor:
>  VOGLIA DI VACANZE?
> * Vieni nei Riccione Family Hotels, gli alberghi specializzati per le
> vacanze dei bambini! Prenota online le nostre offerte
>  Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=9303&d=12-7
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090712/b07c33a8/attachment.html 


More information about the Qt-interest-old mailing list