PDA

View Full Version : QGraphicsScene size



invictus
10th August 2007, 17:54
Hi

I am trying to create a GraphicsView where I can draw using the entire window. To do this I create a GraphicsScene with the view's rect as constructor. The result is that I get scrollbars (although it shouldnt be needed as the scene should be the same size as the view) that are pre-scrolled (why does it scroll by itself?) a few pixels in both directions. What am I doing wrong here?



#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSvgItem>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QGraphicsView *view = new QGraphicsView;
view->resize(640, 480);


QGraphicsScene *scene = new QGraphicsScene(view->rect());
scene->addItem(new QGraphicsSvgItem("ball.svg"));
view->setScene(scene);


view->show();

return app.exec();
}

marcel
10th August 2007, 18:05
QRect is a little weird.
The size is actually with 1 pxiel smaller in each direction( 639x479).
So you have to adjust it a bit.

Also, I used setFixedxSize since if you resize it, the scroll bars appear again.



int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QGraphicsView *view = new QGraphicsView;
view->setFixedSize(640, 480);


QRect r = view->rect();
r.adjust(1,1,-1,-1);
QGraphicsScene *scene = new QGraphicsScene(r);
//scene->addItem(new QGraphicsSvgItem("ball.svg"));
view->setScene(scene);
view->show();
return app.exec();
}


Regards

invictus
10th August 2007, 18:28
QRect is a little weird.
The size is actually with 1 pxiel smaller in each direction( 639x479).


What is the reason for this behaviour? Seems a bit odd to me...

The adjustment didnt fix it for me either

I also get the same behaviour if I use view->width() / height() instead of the rect. And I still think its weird that it decides to autoscroll a couple of pixels.

marcel
10th August 2007, 18:30
The count starts at 0.
Also, the view also has some borders that you must consider, since they are part of the bounding box.
I guess you have to query the current style for the border
width and subtract it from the rect size, in order to get the same results for different styles.

Regards

invictus
10th August 2007, 19:06
The count starts at 0.
Also, the view also has some borders that you must consider, since they are part of the bounding box.
I guess you have to query the current style for the border
width and subtract it from the rect size, in order to get the same results for different styles.

Regards

Btw, will the childrenRect property perhaps give the same as rect minus border/margin/padding?

marcel
10th August 2007, 19:22
Btw, will the childrenRect property perhaps give the same as rect minus border/margin/padding?
Well, I was wrong:
size() and the other functions in this family do not include the widget's borders.
So, you're ok adjusting the rect.

invictus
10th August 2007, 19:56
Well, I was wrong:
size() and the other functions in this family do not include the widget's borders.
So, you're ok adjusting the rect.

I wish I were...but I really can not get this to work. Perhaps there is something wrong with my computer system as I am running gnome to test this? No matter what I do and how adjusted the rect is, the item will always be partially outside the view... but if I can scroll I can scroll it "back" into the view. This is really weird I think

wysota
10th August 2007, 22:39
What is the reason for this behaviour?

Backward compatibility with earlier Qt versions.

invictus
12th August 2007, 21:01
I was wondering...is there any other widget than QGraphicsView I can use for 2D graphics? And can I render SVG/Vector graphics on this (if such a widget exists)? I think perhaps GraphicsView is too complicated for my use when all I want is to do some custom drawings on a given portion of the application window.

My book doesnt cover graphicsview either, while it has a short chapter on 2D using QPainter...

marcel
12th August 2007, 21:13
I was wondering...is there any other widget than QGraphicsView I can use for 2D graphics? And can I render SVG/Vector graphics on this (if such a widget exists)? I think perhaps GraphicsView is too complicated for my use when all I want is to do some custom drawings on a given portion of the application window.

You can render a svg on virtually any paint device by using QSvgRenderer.
This includes widgets and pixmaps. You could use, for example a QWidget, or a QLabel if you wish.




My book doesnt cover graphicsview either, while it has a short chapter on 2D using QPainter...

Because the Graphics View Framework is relatively new.
Try this: http://doc.trolltech.com/4.3/graphicsview.html\

Regards

invictus
12th August 2007, 21:21
If I can do that, why would I use graphicsview to begin with? Sorry if my questions are too stupid to be answered :p

To be honest, what I am trying to do is to create a small puzzle game using SVG images for scalability.

marcel
12th August 2007, 21:26
If I can do that, why would I use graphicsview to begin with?

The disadvantage, as I see it, when using widgets is that you will have to provide your own item interaction implementation( move puzzle pieces around, etc ).

With a QGraphicsView/scene these things are built-in.
Basically, all you have to do is concern on the drawing stuff.

Regards