How to have QGraphicsScene not centered in QGraphicsView - qt

How can I manualy position a QGraphicsScene inside the QGraphicsView, because the default is always centered, but I want to position it by myself.

Call the centerOn function of QGraphicsView
Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view.
Alternatively, if you want to specify a rect of the area in the scene to be visible, you can use fitInView.

Related

How to fix the position of QGraphicsItem and making only the QGraphicsTextItem to move?

I have a QGraphicsView in which I am painting a QGraphicsItem and QGraphicsTextItem.
I have set the flag ItemIsMovable for the QGraphicsTextItem for editing the long text.
Here my concern. When I am moving the QGraphicsTextItem to view the long text, it is also moving the QGraphicsText to left.
But I want only the QGraphicsTextItem to be movable.
I want to fix the position of QGraphicsItem? How do I do that?
Edit:
Here is the output of my code. I have this QGraphicsView at the top-left of my application. The Viewport of this QGraphicsView is the width of the QGraphicsItem.
When I click on the QGraphicsTextItem and move the cursor to the right to view the text, the QGraphicsItem also moves and becomes hidden as shown in the image.
How do I fix the position of this QGraphicsItem?

How to get visible scene rect for QGraphicsView

Imagine QGraphicsView is set with the scene that is twice as wider as view's width. sceneRect() will return the whole scene rect. I'd like to get the rect representing the half of the scene with respect to scrollbars' positions.
Get the viewport rectangle with the graphicsView->viewport()->rect() and map it to the scene space with the graphicsView->mapToScene().
I am not totally sure which rectangle you exactly need (this depends from what you want to do) but something like graphicsView->viewport()->contentsRect() or graphicsView->viewport()->geometry() should work.

Qt - draw inside QScrollArea in a QDialog

In Qt5, I have a QDialog window on which I have drawn a circle as follows:
void MyDialog::paintEvent(QPaintEvent *pe)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
QPen pen(Qt::blue,2);
painter.setPen(pen);
QRect r=QRect(0,0,100,100);
painter.drawEllipse(r);
}
If I draw a larger circle, for example by using QRect(0,0,500,500);, the circle being greater than the dialog window is clipped. So I dragged a QScrollArea onto the the dialog window and decide to draw onto that so that scroll bars are automatically added. The QScrollArea can be accessed using ui->scrollArea.
I changed the above code by setting QPainter painter(ui->scrollArea);. However, nothings appears in the QScrollArea. I read that I need to override the paintEvent of QScrollArea. But I don't know how to do this.
Any help of drawing on the QScrollArea?
Drawing on the QScrollArea is not what you want either because the QScrollArea actually has a viewport widget.
Create another class which inherits QWidget. Override the paintEvent() method and to the painting you mention. Then, add the widget to the scroll area in your dialog.
MyDialog::MyDialog()
{
QScrollArea *pScrl = new QScrollArea(this);
pScrl->setWidget(new MyWidget(pScrl));
... // use a layout to put the scroll area in the dialog
}
To really make it useful you will need to resize the MyWidget instance to the size of the circle that you want to draw.

x,y point shifts when the width is reduced below 200 in QGraphicsScene

I have created a QGraphicsScene
scene = new QGraphicsScene(0,0,200,200);
and draw a line scene.addLine(0,0,100,100,pen); and I got the line correctly drawn from (0,0) to (100,100).
When I change the code to
scene = new QGraphicsScene(0,0,150,200);
And draw the same line scene.addLine(0,0,100,100,pen);, Its x,y point shifts to the right side by 50 pixels. Why does this happen? How to avoid this?
By default, the scene rectangle is centered in the QGraphicsView when it is smaller than the view.
You can use QGraphicsView::setAlignment to change that.
Since the view preferred size, returned by QGraphicsView::sizeHint() is also the scene size, you can adjust the view to fit exactly the scene with:
view->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
which tells the layout to only use sizeHint() to calculate the size of the widget.
Or if the view is not inside a layout, you'll have to set the size with
view->setFixedSize(view->sizeHint());
each time you change the scene rect size.

How to resize a QGraphicsView after QGraphicsView::Scale

Is there non-hideous way to resize a QGraphicsView after a call of scale().
My QGraphicsView is embedded in a QScrollArea. When I zoom i want the View the get bigger.
Thx
QGraphicsView itself inherits from QAbstractScrollArea. Embedding it within another QScrollArea would be very strange, and is likely the source of your problems. By default, when you scale a QGraphicsScene larger than the QGraphicsView in which it is displayed, it will show the necessary scroll bars.

Resources