froz
froz

Reputation: 163

Qgraphicview resizing when windows resizing

I would like to ask you about an issue I do not succeed to fix. I have a QApplication which load an image in the first mainwindow.

The code is the following:

QGraphicsScene *scene = new QGraphicsScene;
QPixmap pixmap(QString::fromStdString("image.png");
scene->addPixmap(pixmap);
ui->graphview->setScene(scene);
ui->graphview->show();

I am trying to fit the image to scale in the QGraphivsView, even when I resized my windows.

However, the image is displayed with its own size at running time and this size is not changing when windows is resized. For example, increasing my windows does not increase the image size and similarly for decreasing.

I tried even by addind the following code:

ui->graphview->fitInView(pixmap, Qt::KeepAspectRatioByExpanding);

But nothing is working.

I provide you an example on the following image of what is happening.

enter image description here

Upvotes: 0

Views: 145

Answers (1)

froz
froz

Reputation: 163

I have found a way however, it is not 100% satisfying. I have replaced the resizeEvent function as on the example below:

void QMainWindows::resizeEvent(QResizeEvent *){
QRectF bounds = ui->graphQSYS->scene()->sceneRect();
ui->graphQSYS->fitInView(bounds, Qt::KeepAspectRatioByExpanding);
ui->graphQSYS->centerOn(bounds.center());
}

However, when resizing, the image quality is becoming really bad. Text written on it cannot be read anymore. Do you know another way to keep picture quality ? Thank you very much.

Upvotes: 0

Related Questions