View Full Version : QGraphicsView and QGraphicsScene
alisami
2nd December 2008, 12:06
Hi,
I have a problem about setting the rectangle size of the QGraphicsScene to QGraphicsView's visible part.
When I set the scene's rectangle using view's width and height, the scroll bars appear.
How can I determine the maximum visible size of the QGraphicsView so that I can set the scene's rectangle to maximum ?
The example code is below to demonstrate the problem:
#include <QApplication>
#include <QtGui>
#include <QDebug>
int main ( int argc, char *argv[] )
{
QApplication app ( argc, argv );
QGraphicsView view;
view.setGeometry ( 10, 10, 400, 300 );
view.setScene ( new QGraphicsScene() );
qint32 gvWidth = view.width();
qint32 gvHeight = view.height();
view.setSceneRect ( -gvWidth / 2, -gvHeight / 2, gvWidth, gvHeight );
QGraphicsLineItem* line = new QGraphicsLineItem ( - gvWidth / 2, - gvHeight / 2, gvWidth / 2, gvHeight / 2 );
view.scene()->addItem ( line );
view.show();
return app.exec();
}
Sami
wysota
2nd December 2008, 13:47
Use the viewport() rect of the view.
alisami
3rd December 2008, 09:54
Actually, I have figured out that there is a problem about the viewport's size when using QMainWindow.
I have created a QMainWindow with a QMenuBar and a QGraphicsView on the central part of the QMainWindow.
When the QMenuBar is created and the `setMenuBar()` method of QMainWindow is called, the size of the resulting viewport of the view is not the same as the viewport's size (there is a 20-30 pixels difference). But if `setMenuBar()` method is not called, everything works just fine.
wysota
3rd December 2008, 10:01
Are you sure it is not your fault? Maybe you are reading the size too early or something? I strongly doubt some external widget would have any influence on some other widget's internal widget.
alisami
4th December 2008, 07:36
wysota,
I hope it is my fault, as I couldn't figure out how to fix this problem.
Below are the files for the case. When commenting out the below line in QGuiMainScreen.cpp, the scrollbars disappears:
this->setMenuBar ( menuBar );
QGuiMainScreen.h
#include <QMainWindow>
#include <QMenuBar>
#include <QGraphicsView>
class QGuiMainScreen: public QMainWindow
{
public:
QGuiMainScreen();
virtual void show();
private:
QGraphicsView* m_gv;
};
QGuiMainScreen.cpp
#include "QGuiMainScreen.h"
#include <QLayout>
#include <QGLWidget>
QGuiMainScreen::QGuiMainScreen() : QMainWindow()
{
QWidget *centralwidget;
QHBoxLayout *outerLayout;
QMenuBar *menuBar;
QMenu *menuSystem;
this->resize ( 1024, 768 );
centralwidget = new QWidget ( this );
this->setCentralWidget ( centralwidget );
menuBar = new QMenuBar ( this );
menuBar->setGeometry ( QRect ( 0, 0, 1024, 30 ) );
menuSystem = new QMenu ( menuBar );
menuSystem->setTitle ( "System" );
menuBar->addAction ( menuSystem->menuAction() );
this->setMenuBar ( menuBar );
outerLayout = new QHBoxLayout ( centralwidget );
outerLayout->setSpacing ( 0 );
outerLayout->setMargin ( 0 );
m_gv = new QGraphicsView ( this );
m_gv->setScene ( new QGraphicsScene() );
outerLayout->insertWidget ( 0, m_gv, 1 );
}
void QGuiMainScreen::show()
{
QMainWindow::show();
m_gv->setViewport ( new QGLWidget() );
qint32 sceneWidth = m_gv->viewport()->width();
qint32 sceneHeight = m_gv->viewport()->height();
m_gv->scene()->setSceneRect ( -sceneWidth / 2, -sceneHeight / 2, sceneWidth, sceneHeight );
}
main.cpp
#include <QApplication>
#include "QGuiMainScreen.h"
int main ( int argc, char *argv[] )
{
QApplication app ( argc, argv );
QGuiMainScreen myWnd;
myWnd.show();
return app.exec();
}
pro file
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
QT += opengl
# Input
HEADERS += QGuiMainScreen.h
SOURCES += main.cpp QGuiMainScreen.cpp
Sami
wysota
4th December 2008, 09:26
First of all you don't have to create a menu bar yourself. If you call QMainWindow::menuBar() a menu bar will be created and setup for you. There is a chance this will already fix your problem.
alisami
4th December 2008, 10:02
Actually, the code is generated by uic and I just copied the generated code to demonstrate the problem.
So when I call `setupUi()` the problem occurs.
alisami
4th December 2008, 10:06
wysota,
I also tried to create the menubar by using QMainWindow::menuBar() but again the viewport do not have the correct size and the problem persists.
sami
alisami
4th December 2008, 10:10
Hi,
It is confirmed by Qt support that this is bug related to the menubar.
Yeah, looks like this is a bug because QMenuBar is reporting it's size wrong as a height of 2 pixels instead of 30 for some reason.
wysota,
Thanks for your interest.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.