Solved Get the size of the widget in the layout
-
I would like to get the size of the QGraphicsView in the grid layout, following is the whole window
The bounding box is the area size I want to get
I try to get the size when windows resize
#include "mainwindow.hpp" #include "ui_mainwindow.h" #include <QDebug> #include <QGraphicsScene> #include <QResizeEvent> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); gscene_ = new QGraphicsScene(this); ui->graphicsView->setScene(gscene_); } MainWindow::~MainWindow() { delete ui; } void MainWindow::resizeEvent(QResizeEvent *event) { qDebug()<<ui->gridLayout->layout()->itemAt(0)->widget()->size(); //not the size I want qDebug()<<event->size(); //this is the size of full window //size of the scene will be too large if I set the size like this //gscene_->setSceneRect(0, 0, event->size().width(), event->size().height()); QMainWindow::resizeEvent(event); }
But I either get the full size of the window or wrong size of the widget, how could I get the size of the bounding boxes I draw?Thanks
-
why you dont try
ui->graphicsview->size();
?
Your Graphicsview is a Widget that has a Objectname in your UI. Try this.
-
@Fuel Thanks, situation become better, but it still got some extra space left
There scene do not just fit but has some space left(scroll bar). How could I figure out the scene rect as this bounding box show(without generate any scroll bar)?
void MainWindow::resizeEvent(QResizeEvent *event) { qDebug()<<"layout size:"<<ui->gridLayout->layout()->itemAt(0)->widget()->size(); //not the size I want qDebug()<<"resize event size:"<<event->size(); //this is the size of full window qDebug()<<"graphics view size:"<<ui->graphicsView->size(); gscene_->setSceneRect(0, 0, ui->graphicsView->size().width(), ui->graphicsView->size().height()); QMainWindow::resizeEvent(event); }
-
You dont want the Scollbars? Maybe that is what you are looking for http://doc.qt.io/qt-4.8/qgraphicsview.html#fitInView