0

I have found this: Getting the size of a QGraphicsView

But I can't figure out what does it mean to "move my initialization code to showEvent" and I can't comment on that answer.

I am want to resize a QPixmap so it could fit my QGraphicsView. I've placed my graphicsview in Designer and set GridLayout for my main window. In a MainWindow constructor I have written the following code: ui->setupUi(this);

// Get GView size
g_sizeX = ui->mapView->width();
g_sizeY = ui->mapView->height();
// Init  scene
scene = new QGraphicsScene(this);
// Init MAP pixmap and add it to scene
mapImage = new QPixmap(":/Map/europe.jpg");
QPixmap newmapImage = mapImage->scaled(g_sizeX, g_sizeY);
scene->addPixmap(newmapImage);

// Display scene in gview.
ui->mapView->setScene(scene);

But I always get size of 100x30. If I break the gridLayout, I get the correct size. So, how should I deal with this?

Thank you.

Community
  • 1
  • 1
KarolisL
  • 349
  • 1
  • 10

2 Answers2

0

I believe that what is happening is that Qt only applies layouts and sets widget sizes when the widget is first displayed.

One way to work that is to override QWidget::showEvent(), and put your sizing code in there.

However, one simpler way, that often works in constructors, is to ask the widget for its sizeHint(), rather than for its not-yet-layed-out size.

In your case, that would mean changing two lines of code to:

g_sizeX = ui->mapView->sizeHint().width();
g_sizeY = ui->mapView->sizeHint().height();

If your layout isn't too complicated, and if you haven't overridden the default size policies, this may well fix things for you.

Clare Macrae
  • 3,670
  • 2
  • 31
  • 45
0

The QGraphicsView will be resized by the QGridLayout after the widget is shown, and can be also resized later when the window is itself resized.

So you should change the size of the pixmap as a result of a QResizeEvent, either by subclassing QGraphicsView to redefine resizeEvent(), and then promoting your view object to your new class in the designer to use it instead of QGraphicsView, or by installing your MainWindow object as an event filter for the view to handle to the resize event from the MainWindow::eventFilter function.

You probably don't want to change the pixmap size in the scene, but rather adjust the view matrix so that your QGraphicsPixmapItem fits perfectly inside the view, with QGraphicsView::fitInView.

For example:

/* QGraphicsPixmapItem *pixmapItem; as a MainWindow member */
pixmapItem = scene->addPixmap(newmapImage);

/* Either always disable or enable the scrollbars (see fitInView doc) */
ui->mapView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->mapView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

ui->mapView->installEventFilter(this);
...
bool MainWindow::eventFilter(QObject *obj, QEvent *evt) {
     if(obj == ui->mapView && evt->type() == QEvent::Resize) { 
          ui->mapView->fitInView(pixmapItem, Qt::KeepAspectRatioByExpanding);
     }
     // Call the base class implementation 
     return QMainWindow::eventFilter(obj, evt);
}
alexisdm
  • 29,448
  • 6
  • 64
  • 99