PDA

View Full Version : Size of QGraphicsView



IsleWitch
10th October 2007, 11:24
Hi,

Is there anyway in which I could set the size of QGraphicsView. I would like it to be, as in the image attached.

Thank You.

rajesh
10th October 2007, 11:31
you can get it using
size()
or
QRectF sceneRect ()

IsleWitch
10th October 2007, 12:03
Thanks Rajesh.

But size(), doesnt work. I used QRectF sceneRect (), but, no apparent changes occured. The graphicsView stayed the same size, it's occupying the window (as in, there are no spaces at the sides of the graphicsViewer).

wysota
10th October 2007, 12:10
Are you using layouts in your window?

rajesh
10th October 2007, 12:16
its not clear what you want to do?
if you want to set initial size of QGraphicsview then you can use sizeHint.

or

QGridScene m_scene = new QGraphicsScene(0,0,1000,600,this); // define size here
graphicsView.setScene(m_scene);

actually you can not display directly to QGraphicsView, you need to use QGraphicsScene
eg.
QGraphicsScene scene;
scene.addText("Hello, world!");
QGraphicsView view(&scene);
view.show();

no, I am not using layout. but I am writing hand coding. no Qt Designer.

IsleWitch
10th October 2007, 12:16
Yes I'am using layouts.

Here Is the code, it might give a better picture of what im trying to do:


#include <QLabel>
#include <QPushButton>
#include <QGraphicsView>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QApplication>
#include <QCheckBox>
#include "browseimages.h"

BrowseImages::BrowseImages(QWidget *parent) : QWidget(parent)
{
// Title of the window
this->setWindowTitle("Browse Images");
this->resize(700, 400); // I'm using your old values

b_search = new QPushButton(tr("SEARCH"));
b_search->setFixedHeight(40);

b_exit = new QPushButton(tr("Exit"));
b_exit->setFixedHeight(40);

QHBoxLayout *h_exit = new QHBoxLayout;
h_exit->addStretch();
h_exit->addWidget(b_exit);
h_exit->addStretch();

connect(b_exit, SIGNAL(clicked()), qApp, SLOT(quit()));

l_welcome = new QLabel(tr("--Browse Images--"));
l_welcome->setFont(QFont("Times", 18, QFont::Bold));
l_welcome->setAlignment(Qt::AlignCenter);

g_graphics = new QGraphicsView(this);
g_graphics->scale(50,70);


QHBoxLayout *v_1 = new QHBoxLayout;
v_1->setAlignment(Qt::AlignCenter);
v_1->addSpacing(30);
v_1->addWidget(b_search);


QHBoxLayout *v_e = new QHBoxLayout;
v_e->setAlignment(Qt::AlignCenter);
v_e->addWidget(b_exit);


QVBoxLayout *v_g = new QVBoxLayout;
v_g->setAlignment(Qt::AlignCenter);
v_g->addWidget(g_graphics);


QVBoxLayout *layout_vert = new QVBoxLayout;
layout_vert->setAlignment(Qt::AlignTop);

layout_vert->addWidget(l_welcome);
layout_vert->addSpacing(30);
layout_vert->addLayout(v_g);
layout_vert->addSpacing(20);
layout_vert->addLayout(v_1);
layout_vert->addSpacing(20);
layout_vert->addLayout(v_e);
//layout_vert->addLayout(h_exit);


this->setLayout(layout_vert);

}

Thank You,

rajesh
10th October 2007, 12:31
ok, let me check your code.

wysota
10th October 2007, 12:31
Oh God, please don't use fixed sizes :) Attached is the Designer mockup of what you (probably) want. You can either use uic to generate the code used to obtain such a layout or just write the code yourself based on what you see. You might also get rid of those two external spacers as they don't really fit there preventing the graphics view from expanding. It's better to ask the dialog to adjust its just to the graphics view and not to force that empty space on both sides.

rajesh
10th October 2007, 12:34
if you using designer then you can add spacers both side.

rajesh
10th October 2007, 12:58
Is your problem solved?

if you doing hand coding then you can use QGridLayout which is easy to set all widget.
like

{
// Title of the window
setWindowTitle("Browse Images");
resize(700, 400); // I'm using your old values

QPushButton *b_search = new QPushButton(tr("SEARCH"));
QPushButton *b_exit = new QPushButton(tr("Exit"));
QLabel *l_welcome = new QLabel(tr("--Browse Images--"));
l_welcome->setFont(QFont("Times", 18, QFont::Bold));
l_welcome->setAlignment(Qt::AlignCenter);

QGraphicsView *g_graphics = new QGraphicsView(this);
g_graphics->scale(50,70);
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(l_welcome,0,2,1,4);
gridLayout->addWidget(g_graphics,1,2,4,5);
gridLayout->addWidget(b_search,6,3);
gridLayout->addWidget(b_exit,7,3);
gridLayout->setColumnMinimumWidth(0,50);
gridLayout->setColumnMinimumWidth(7,50);
setLayout(gridLayout);
connect(b_exit, SIGNAL(clicked()), qApp, SLOT(quit()));
}

IsleWitch
10th October 2007, 13:09
Thank You very much Rajesh and Wysota, I'am still working on it. I'm trying your suggestions, and i will get back to you as soon as i ge the desired result.

Thanks again for your help :)