Hi!
I'm having trouble with a QGraphicsView. Basically, I want to draw some items (a circle in this example) in a way, that they always fill the complete view.
I did put the QGraphicsView on the main window using qtcreators designer and use it with the following mainwindow.cpp (full example in as attachement):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPen>
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
circle
->setPen
(QPen(Qt
::black));
scene.addItem(circle);
ui->graphicsView->setScene(&scene);
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPen>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
circle = new QGraphicsEllipseItem(-10, -10, 20, 20);
circle->setPen(QPen(Qt::black));
scene.addItem(circle);
ui->graphicsView->setScene(&scene);
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent *e) // virtual
{
ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
QMainWindow::resizeEvent(e);
}
To copy to clipboard, switch view to plain text mode
Now what happens is when the window opens, the items do not fill the whole QGraphicsView, they are too small:
initial_view.png
But after the first resize event the fitInView function works as expected and the items fill the whole space:
after_resize.png
Does somebody have an idea why this happens and how to avoid it?
Regards,
syrtyr
Bookmarks