Important: Please read the Qt Code of Conduct -

Initialize QGraphicsScene

  • Hello to all,

    I have subclassed a QGraphicsScene in order to display an image in a QGraphicsView.But I initialize the Canvas as it seems bellow in the canvas.cpp file

    @Canvas::Canvas(): QGraphicsScene(0,0,128,128)

    where 128x128 is the size of the displayed image.In my mainwindow.cpp file I have created an object
    @Canvas *scene;@

    How could I set the size of the scene in my mainwindow?In this case the constructor should be like this?

  • Size @QGraphicsScene@ or @QGraphicsView@ ?

  • I want to set the size of scene which is subclassed from QGraphicsScene if I know the width and length of this scene.
    I want to use this


    and not this to initialize the constructor of canvas
    @Canvas::Canvas(): QGraphicsScene(0,0,128,128)

    where Canvas *scene;

  • setSceneRect()

  • To be more specific I use a push button event and every time I press the button I run this function.But ecery time I am loading first a small image and then a bigger, everything is fine.When I am loading first a bigger image and then a smaller,then I do not take the image scaled in the hole QGraphicsView, but it is displayed in the middle of the QGraphicsView without been scaled.Could somebode help me to solve this problem?

    @void MainWindow::push_button_File(void)

    QPixmap tmpmap (fileName, 0, Qt::AutoColor);
    if (!pixmapItem) {
    pixmapItem = scene->addPixmap (tmpmap);
    ui->graphicsView_inputImage ->height()/scene->height());
    } else {

    } @


  • @Canvas::Canvas(int x, int y, int width, int height) : QGraphicsScene(0,0,128,128)


    And set the screen to the view with

    @Canvas *scene;

    But I wonder whey do you want to create a class Canvas. You can directly create QGraphicsScene with what ever size you want and directly set it to QGraphicsView.

    @ QGraphcisView view;
    view->setScene(new QGraphicsScene(0,0,128.128); @

  • I have created canvas to insert a mouse press event.I have managed to get some results but they are not totaly right. Have a look at my previous post in the same topic

Log in to reply