PDA

View Full Version : No QGraphicsScene proper coordinates



harmodrew
14th November 2010, 17:12
Hello!

I'd like to assign a QGraphicsScene to a QGraphicsView which is set as the central widget of the main window.

Moreover the scene coordinate system should be set as the view inner space...I wrote the following code:

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsTextItem>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

scene = new QGraphicsScene;
view = new QGraphicsView;
view->setScene(scene);
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;

setCentralWidget(view);

scene->setSceneRect(view->geometry());
view->setFocus();

showLine();
}

void MainWindow::showLine()
{
scene->addLine(0, 0, 100, 100);
}

MainWindow::~MainWindow()
{
delete ui;
}



(scene and view are two MainWindow object's attributes)

but, running this code the line is drawn about at the center of the view...why doesn't the line start at the top left corner?

Thanks

wysota
14th November 2010, 22:07
See what view->geometry() returns in the constructor and you'll be able to answer your question yourself.

harmodrew
15th November 2010, 08:02
Hi

I'm not able to find the error...I wrote this and now it works


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

view = new QGraphicsView;

view->setGeometry(0, 0, 300, 300);
view->setParent(this);

}

void MainWindow::drawLine()
{
scene = new QGraphicsScene;
view->setScene(scene);
scene->setSceneRect(view->rect());
scene->addLine(0, 0, 100, 100);
}


but why the previous code doesn't work?

Thanks

wysota
15th November 2010, 09:57
but why the previous code doesn't work?
Did you check what geometry() returns from within the constructor?

harmodrew
15th November 2010, 10:12
it returns me a QRect with this values:



x1 = 0
x2 = 639
y1 = 0
y2 = 479

wysota
15th November 2010, 11:29
it returns me a QRect with this values:

Could you please show us how you got those values (including the whole method body where you added your call)?

harmodrew
15th November 2010, 11:39
mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsTextItem>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

scene = new QGraphicsScene;
view = new QGraphicsView;
setCentralWidget(view);

view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
view->setScene(scene);
aux = view->geometry(); // AFTER DEBUGGING THIS ROW: AUX = 640x480+0+0
}

void MainWindow::drawLine()
{
scene->addLine(0, 0, 100, 100);
}

MainWindow::~MainWindow()
{
delete ui;
}



main.cpp


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showMaximized();
w.drawLine(); // THE LINE WON'T START AT THE TOP LEFT CORNER
return a.exec();
}


mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsView>
#include <QGraphicsScene>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindow *ui;
QGraphicsView *view;
QGraphicsScene *scene;
QRect aux;

public:
explicit MainWindow(QWidget *parent = 0);
void drawLine();
~MainWindow();
};

#endif // MAINWINDOW_H

wysota
15th November 2010, 13:14
You are not setting the scene size anywhere so why would the line be in the top-left corner? By default the scene gets the boundingRect of all its items and is centered in the view.

harmodrew
15th November 2010, 13:51
I added this row


scene->setSceneRect(aux);


after the "aux row" but I gain the same result

wysota
15th November 2010, 14:01
I'm sure you get a different result but not the one you would probably like to get. You are showing the window maximized but I'm pretty sure the size of your screen is not even close to 640x480, right? So your scene is 640x480 but your view is much bigger. Correct?

harmodrew
15th November 2010, 14:11
yes, you're right..but if I run the code at post #3 it draws the line at the right coords

wysota
15th November 2010, 15:23
You are setting the scene to the wrong size because in the constructor the size of the view is not correct. You can see that in the figures you gave me, right?

Even if it worked then still trying to match the scene size with the view size is just a sign you don't understand how Graphics View architecture works. The scene operates in logical coordinates which are independent of how the scene is displayed in the view. The scene should have a fixed size and the view can zoom in on it so that it covers the whole area of the view's viewport.

harmodrew
15th November 2010, 17:43
so I should assign a scenerect to the scene at the startup and then the view will scale oll the graphical items into the scene?

wysota
15th November 2010, 18:09
and then the view will scale oll the graphical items into the scene?
If you tell it to do so, yes.

harmodrew
16th November 2010, 09:35
And how can I tell the view to "stretch" the scene object to the view's viewport?
for example if I assign a scenerect of 100x100 with the top letf corner as (0,0) to the scene and then draw a line from 0,0 to 100,100, I'd like the view to show the diagonal of the screen (so the scene is shown entirely into the view and is "stretched" into the view")...

Thanks

wysota
16th November 2010, 12:04
And how can I tell the view to "stretch" the scene object to the view's viewport?
I suggest you look at the available API of QGraphicsView.