0

This seems trivial, but I didn't found a solution. All I want to do is to set up a coordinate system a QGraphicsScene [-10, -10] to [10, 10] (f.e. with setSceneRect(-10, -10, 20, 20)) and to plot a QRect within the scene, using the coordinate system of the scene.

scene = new QGraphicsScene(-10.0, -10.0, 20.0, 20.0, ui->graphicsView);
// scene is already a QGraphicsScene pointer defined in the .h
ui->graphicsView->setScene(scene);
scene->addRect(-8, -8, 4, 4);

What I am expecting in the example is a rectangle bounding at the left-bottom border. I also tried mapToScene() and fitInView(), but that also didn't brought me the expected result.

Thanks for helping.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
braggPeaks
  • 1,158
  • 10
  • 23

1 Answers1

2

I think you are looking for QGraphicsView::setSceneRect() which tells the view what part of the scene to display.

Steffen
  • 2,888
  • 19
  • 19
  • Hello Steffen. I also tried setSceneRect() of QGraphicsView. But as the documentation states: "If unset, or if a null QRectF is set, this property has the same value as QGraphicsScene::sceneRect, and it changes with QGraphicsScene::sceneRect." If I display current values for sceneRect() (with qDebug() << scene->sceneRect(); ) I do get the same and correct values for both, QGraphicsView and QGraphicsScene. – braggPeaks Mar 12 '12 at 09:04
  • Hmm, that's interesting, I just tried your example and up to now the only combination that is working for me is: fitInView() somewhen after construction (button handler for example) + setViewportUpdateMode(QGraphicsView::FullViewportUpdate). FWIW adding a Rectangle at (-10,-10,20,20) seems to help, too. – Steffen Mar 12 '12 at 09:50
  • Sorry, could you please provide a code snippet, since I don't get it to work correctly. EDIT: I can remember that when I used fitInView(), it was just a zoom-view of that specific area, but does not set the dimensions of the scene. As a result, the lines of the rectangles were > 1 px (because of the zoom) and there were scrollbars generated (which I don't need). – braggPeaks Mar 12 '12 at 10:03
  • I set up a project (Qt Gui Application), added a QGraphicsView and a QButton to the UI, put your code in the constructor of MainWindow and added a click handler for the button: void MainWindow::on_pushButton_clicked() { ui->graphicsView->fitInView(-10.0,-10.0,20.0,20.0); } Then after pressing the button, the view looks as I would expect. – Steffen Mar 12 '12 at 10:08
  • First, thanks for your help. That also worked here as I wanted, but why do you need the pushButton? I tried ui->graphicsView->fitInView(-10.0,-10.0,20.0,20.0); in the constructor, just after creating the rectangle, but that doesn't work?! – braggPeaks Mar 12 '12 at 10:15
  • 1
    Sorry, I don't know exactly why the behaviour is like that. It might be a bug or just related to the fact that the event loop has to run before the view setup really takes place?! – Steffen Mar 12 '12 at 13:57