PDA

View Full Version : how to center picture in graphicsview?



lanmanck
16th August 2009, 12:20
hi:
i use following code to display my picture:

QPixmap *pm = new QPixmap(picPath);
QGraphicsScene *gs = new QGraphicsScene();
gs->addPixmap(*pm);
ui.graphicsView.setScene(gs);

the picture is displayed in the center of graphicsview by default,
but,when i load bigger one into the graphicsview,and then load a smaller one again, the smaller one will display in the top-left coordinate, here are some snapshots:

first:
http://www.qtcn.org/bbs/attachment/Mon_0908/17_48171_9fa2a782e1a9f96.gif
load big one:
http://www.qtcn.org/bbs/attachment/Mon_0908/17_48171_774402ce45a1034.gif
load small again:
http://www.qtcn.org/bbs/attachment/Mon_0908/17_48171_ac3d315a0eed567.gif
i have set fixinview(0,0,320,240); and the scrollbar is gone, the big ones display well, but the small ones.....

tommy
16th August 2009, 19:29
Maybe this will help you:

http://www.forum.nokia.com/infocenter/index.jsp?topic=/Qt_for_S60_Developers_Library/GUID-ECBE8350-9D54-48D1-B777-264B895B9063/sql-drilldown.html

lanmanck
17th August 2009, 03:58
thanks ,i will try it.

aamer4yu
17th August 2009, 20:38
You can also have a look at QGraphicsView::fitInView , QGraphicsView::ensureVisible and QGraphicsView::centerOn :)

lanmanck
18th August 2009, 17:09
hi,aamer4yu:
i do it but not effect,here is my code:
QPixmap *pm;
QGraphicsScene *gs;
.......
void PhotoView::showPhotoInGraphicsView(QString pic)
{
pm->load(pic);
gs->clear();
gs->addPixmap(*pm);

m_ui->graphicsView->centerOn(320,240); //no effect
//m_ui->graphicsView->translate(160,120); // no effect
m_ui->graphicsView->fitInView(0,0,m_ui->graphicsView->width(),m_ui->graphicsView->height());
m_ui->graphicsView->setScene(gs);
}

aamer4yu
19th August 2009, 12:09
What if you do something like -

QGraphicsPixmapItem *pPix = gs->addPixmap(*pm);
m_ui->graphicsView->fitInView(pPix);

does it work :rolleyes:

lanmanck
23rd August 2009, 04:08
hi,aamer4yu:

it seems to work, but i don't want the small ones also scale to fix the view:

http://qtcn.org/bbs/attachment/Mon_0908/17_48171_c2368d07cbb57b7.gif

i have try these code,but no change : :(

if( (pm->height() >= m_ui->graphicsView->height()) || (pm->width() >= m_ui->graphicsView->width()) )
m_ui->graphicsView->fitInView(pPix);

i also read fixinview() document, the enum qt::aspectratiomode(or others) cannot let the small ons not scale to fix the view?

aamer4yu
23rd August 2009, 10:10
it seems to work, but i don't want the small ones also scale to fix the view:
May be then you want some fixed area to be shown in the view.
Currently if u apply fitinview to a item, it will scale the view to show that item.

So what you can do is - decide over a area size you want to show(say 300x200) ,,get the cordinate of the item, compare the size ,,,, compute the final rect
and then call fitInView . theres an overloaded fitinView for fitting a given scene rect :)

lanmanck
25th August 2009, 16:31
thanks .
but i am a newbie and have no idea how to write that code, would you give some more tips?

opra
27th August 2009, 12:51
hi

i have done it with QGraphicsTextItem,

QGraphicsSceneItem* item;

item = scene.addText(.........);
int centerPos_X = POS_X - ((POS_X - POS_Y)/2);
int posY =10;
item->setPos(centerPos_X - item->boundingRect().width() / 2, posY);

lanmanck
27th August 2009, 17:31
i cannot figure it out but using QPainter:

QRectF target;
QRectF source;
QImage image;
QPainter painter;
void PhotoView::drawpic()
{
paintEvent(0);
}
void PhotoView::paintEvent(QPaintEvent*pPaintEvent)
{
target.setX(10.0);
target.setY(20.0);
target.setWidth(80.0);
target.setHeight(60.0);

source.setX(0.0);
source.setY(0.0);
source.setWidth(59.0);
source.setHeight(60.0);

image.load(g_po_t->photopath[g_po_t->currentid ]);

painter.begin(this);
painter.drawImage(target, image, source);
painter.end
}
however, the picture can only display onetime. when i press button to change the picture id, it clews:

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::end: Painter not active, aborted
QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::end: Painter not active, aborted

why?

lanmanck
28th August 2009, 08:09
i have finished it,although not perfect:

http://blog.csdn.net/lanmanck/archive/2009/08/28/4493204.aspx