2 Attachment(s)
Why changing x,y of rect in scene doesn't do anything?
I'm using the simple example from tutorial to show an rectangle on a scene.
As I understand the axises are in the middle of the graphicalView, right?
What I don't understand is why changing x,y of rectangle doesn't do anything.
Here is the code
Code:
QtGuiApplication1
::QtGuiApplication1(QWidget *parent
){
ui.setupUi(this);
ui.graphicsView->setBackgroundBrush(Qt::red);
ui.graphicsView->setScene(_graphicsScene);
QPen blackPen
(Qt
::black);
blackPen.setWidth(6);
_graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
}
The result
Attachment 13087
Even if I change x,y of the rect to
Code:
addRect(50, 50, 100, 100, blackPen, redBrush)
or
Code:
addRect(10000, 10000, 100, 100, blackPen, redBrush)
it shows the same position of the rectangle. Why?
Attachment 13088
Re: Why changing x,y of rect in scene doesn't do anything?
You only have a single item, so the view "centers" on it.
Cheers,
_
Re: Why changing x,y of rect in scene doesn't do anything?
Quote:
Originally Posted by
anda_skoa
You only have a single item, so the view "centers" on it.
Cheers,
_
But, how I position the rectangle at specific location then? I tried
Code:
rectangle = _graphicsScene->addRect(50, 50, 100, 100, blackPen, redBrush);
rectangle->setPos(10, 20);
But, it still position the rectangle in the center of the scene.
What I want to do after I figure out the how the coordinates works (I've read this btw) is to show an image and then add rectangles on an image at specific locations. Those rectangels will be interactive.
Re: Why changing x,y of rect in scene doesn't do anything?
No, it places the rectangle in the scene at the location you supplied. When the view is shown, the view tries to do something sensible with the scene to map it to the available display coordinates. This may result in scaling and/or centreing the scene within the viewport. So, regardless of the scene coordinates, the single rectangle may appear in the same position in the viewport in spite of different scene coordinates.
As to moving the rectangle, here is an example that moves the rectangle after the view is shown.
Code:
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
{
Q_OBJECT
public:
{
_graphicsScene->setBackgroundBrush(Qt::blue);
// resize(800, 800);
setScene(_graphicsScene);
QPen blackPen
(Qt
::black,
6);
_rect = _graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
_graphicsScene
->addLine
( 0,
1000,
0,
-1000,
QPen(Qt
::white));
_graphicsScene
->addLine
(1000,
0,
-1000,
0,
QPen(Qt
::white));
// Make the square move 3 seconds from now
QTimer::singleShot(3000,
this,
&MyView
::moveItem);
}
private slots:
void moveItem() { _rect->moveBy(100, 100); }
};
int main(int argc, char *argv[])
{
MyView view;
view.show();
return a.exec();
}
#include "main.moc"
Uncomment line 17 to see how the initial view changes for the same scene.