PDA

View Full Version : Problem with QGraphicsView/Scene coordinates



Andrewshkovskii
16th October 2009, 12:33
Hello all!
I have problem with changing position for QGraphicsTextItem's on QGraphicsView/Scene. I want pos them at upper left corner, here is the source of my method :



void Model::setUpGraphicScene(QStandardItemModel *model,int size)
{
static QFont font("Times",9);
int x = - this->vrptr->geometry().width(); //vrptr - pointer to QGraphicsView
int y= - (this->vrptr->geometry().height()/2);
QGraphicsItem * item;
static QFontMetrics fn (font);
static int yGrow = fn.height() - 3;
this->visualResultModel->clear();//visualResultModel is a QGraphicsScene
item= visualResultModel->addText(model->horizontalHeaderItem(0)->text(),font);
item->setPos(x,y);
for(int i=1;i<size;++i)
{
item= visualResultModel->addText(model->horizontalHeaderItem(i)->text(),font);
item->setPos(x,y+yGrow);
y=item->pos().y();
}
}

here is result :
http://img26.imageshack.us/img26/491/close2.png
i.e. there is scrolling appears, why?
Thanks.
And sorry for my bad english:)

high_flyer
16th October 2009, 12:52
Well, the first thing that comes to mind is that one of the strings is very long, probably filled with spaces or tabs - can you check and make sure this is not the case?

Oh and why do you asign your x so:

int x = - this->vrptr->geometry().width();

and not just x= 0; ?

it might be that you are placing your string out side the initially visible scene, hence the scroll bar appears.

Andrewshkovskii
16th October 2009, 13:14
Well, the first thing that comes to mind is that one of the strings is very long, probably filled with spaces or tabs - can you check and make sure this is not the case?

Oh and why do you asign your x so:

int x = - this->vrptr->geometry().width();

and not just x= 0; ?

it might be that you are placing your string out side the initially visible scene, hence the scroll bar appears.

No,all strings does not have spaces or tab, and case etc..
x...hm i think, if i assign x=0 it will be at center of view,no?

high_flyer
16th October 2009, 13:25
x...hm i think, if i assign x=0 it will be at center of view,no?
It depends on how you set your coordinate system.
But even if they apear in the middle, it is still in the visible scene area, and if my guess is correct, you will not get the scroll bar.
Make sure you place the string in side the visible scene area, then the scroll bar will not appear.

Andrewshkovskii
16th October 2009, 13:33
It depends on how you set your coordinate system.
But even if they apear in the middle, it is still in the visible scene area, and if my guess is correct, you will not get the scroll bar.
Make sure you place the string in side the visible scene area, then the scroll bar will not appear.

I'm sorry, you did not understand me a little, i want pos my items' at upper left corner without scrollbar:)
And how to set the coordinate system?
I wish that would be the upper left corner was 0.0

Andrewshkovskii
16th October 2009, 14:28
So, anyone can help me?

high_flyer
16th October 2009, 15:07
Regarding your x position, why are you not setting it to half of the width like you did with the height?:

int x = - this->vrptr->geometry().width()/2;

Andrewshkovskii
16th October 2009, 16:07
Regarding your x position, why are you not setting it to half of the width like you did with the height?:

int x = - this->vrptr->geometry().width()/2;

Becouse, when i set it to -half... :

http://img408.imageshack.us/img408/2481/26068397.png

high_flyer
16th October 2009, 16:28
ok, so now as you see, the scroll bar is not appearing any more (your original problem).
So the only thing you have to do now is to figure out the correct position coordinates, such, that you don't position your strings outside the visible scene area and all is well. :)

EDIT:
it looks like the scene is larger then your QGraphicsView, which makes the center of the scene more to the right of the viewing area.
Resize the scene to the size of the viewing area, and then

int x = - this->vrptr->geometry().width()/2;

should work.

Andrewshkovskii
16th October 2009, 16:34
ok, so now as you see, the scroll bar is not appearing any more (your original problem).
So the only thing you have to do now is to figure out the correct position coordinates, such, that you don't position your strings outside the visible scene area and all is well. :)

The incorrect coordinates/position was my problem first:)
either because of no knowledge of English, either because of stupidity, I can not understand your 2nd sentence:)) You mean,what i pos my string outside the the visible scene first time?But what i need to do, to solve the problem with position strings at upper lefr corner?M.b i need to set QGraphicsView::setSceneRect to View's rect? or something else?

Andrewshkovskii
16th October 2009, 16:35
oh...so setSceneRect, as i think,will solve my problem ,yeath?

Andrewshkovskii
16th October 2009, 17:07
if i do like this :


void Model::setUpGraphicScene(QStandardItemModel *model,int size)
{
this->visualResultModel->setSceneRect(this->vrptr->rect());
static QFont font("Times",9);
int x = - (this->vrptr->geometry().height()/2);
int y= - (this->vrptr->geometry().height()/2);
QGraphicsItem * item;
static QFontMetrics fn (font);
static int yGrow = fn.height() -3;
this->visualResultModel->clear();
item= visualResultModel->addText(model->horizontalHeaderItem(0)->text(),font);
item->setPos(x,y);
for(int i=1;i<size;++i)
{
item= visualResultModel->addText(model->horizontalHeaderItem(i)->text(),font);
item->setPos(x,y+yGrow);
y=item->pos().y();
}

}

That's what happens

http://img79.imageshack.us/img79/1020/85489138.png
i.e items pos() out of viewport area

scascio
19th October 2009, 10:41
if i do like this :


void Model::setUpGraphicScene(QStandardItemModel *model,int size)
{
this->visualResultModel->setSceneRect(this->vrptr->rect());
static QFont font("Times",9);
int x = - (this->vrptr->geometry().height()/2);
//Are your sure it is correct to set a x coordinate with height()?
...
}


I think you need to re-thinking your item management, since you seems to not use graphics features as usual way.

QGraphicsView is a view of a QGraphicsScene.
When unsing QGraphicsItem::setPos, you are positionning item in the scene as data regarless of the view portion of the scene displayed in the view.
So the point(0,0) is the center of the scene, whatever shows the view.

To put your item at topleft corner of the view, you can use QGraphicsView::mapToScene to convert pixel coordinates of the viewport to scene coordinates. see the-graphics-view-coordinate-system (http://doc.trolltech.com/4.5/graphicsview.html#the-graphics-view-coordinate-system)

But if you dont move the view, every item will be at same position!

But this still not explain why your sceneRect grows in y-axis...