PDA

View Full Version : Why changing x,y of rect in scene doesn't do anything?



theateist
19th April 2019, 19:03
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


QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);

ui.graphicsView->setBackgroundBrush(Qt::red);
_graphicsScene = new QGraphicsScene(this);
ui.graphicsView->setScene(_graphicsScene);


QBrush redBrush(Qt::yellow);
QPen blackPen(Qt::black);
blackPen.setWidth(6);
_graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
}


The result
13087

Even if I change x,y of the rect to
addRect(50, 50, 100, 100, blackPen, redBrush) or
addRect(10000, 10000, 100, 100, blackPen, redBrush) it shows the same position of the rectangle. Why?
13088

anda_skoa
19th April 2019, 19:18
You only have a single item, so the view "centers" on it.

Cheers,
_

theateist
19th April 2019, 20:08
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

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 (https://doc.qt.io/qt-5/graphicsview.html#the-graphics-view-coordinate-system)) is to show an image and then add rectangles on an image at specific locations. Those rectangels will be interactive.

ChrisW67
20th April 2019, 06:56
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.


#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>

class MyView: public QGraphicsView
{
Q_OBJECT
QGraphicsScene *_graphicsScene;
QGraphicsRectItem *_rect;

public:
explicit MyView(QWidget *p = nullptr): QGraphicsView(p), _graphicsScene(nullptr), _rect(nullptr)
{
_graphicsScene = new QGraphicsScene(0, 0, 500, 500, this);
_graphicsScene->setBackgroundBrush(Qt::blue);
// resize(800, 800);
setScene(_graphicsScene);

QBrush redBrush(Qt::red);
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[])
{
QApplication a(argc, 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.