Results 1 to 12 of 12

Thread: how to center picture in graphicsview?

  1. #1
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Unhappy how to center picture in graphicsview?

    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:

    load big one:

    load small again:

    i have set fixinview(0,0,320,240); and the scrollbar is gone, the big ones display well, but the small ones.....
    Last edited by lanmanck; 16th August 2009 at 13:51. Reason: edit

  2. #2
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to center picture in graphicsview?

    Use computer programming to do science!
    www.scienceprogramming.com

  3. #3
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: how to center picture in graphicsview?

    thanks ,i will try it.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to center picture in graphicsview?


  5. #5
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Unhappy Re: how to center picture in graphicsview?

    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);
    }

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to center picture in graphicsview?

    What if you do something like -
    Qt Code:
    1. QGraphicsPixmapItem *pPix = gs->addPixmap(*pm);
    2. m_ui->graphicsView->fitInView(pPix);
    To copy to clipboard, switch view to plain text mode 

    does it work

  7. #7
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question Re: how to center picture in graphicsview?

    hi,aamer4yu:

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



    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?
    Last edited by lanmanck; 23rd August 2009 at 03:12.

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to center picture in graphicsview?

    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

  9. #9
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: how to center picture in graphicsview?

    thanks .
    but i am a newbie and have no idea how to write that code, would you give some more tips?

  10. #10
    Join Date
    Oct 2008
    Location
    Switzerland
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to center picture in graphicsview?

    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);

  11. #11
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Unhappy Re: how to center picture in graphicsview?

    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?
    Last edited by lanmanck; 27th August 2009 at 16:31. Reason: becouse

  12. #12
    Join Date
    Aug 2009
    Posts
    28
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: how to center picture in graphicsview?

    i have finished it,although not perfect:

    http://blog.csdn.net/lanmanck/archiv...8/4493204.aspx

  13. The following user says thank you to lanmanck for this useful post:

    thaihoangluu (22nd December 2011)

Similar Threads

  1. Print RichText and a scaled Picture
    By kray in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2008, 20:56
  2. Newbie: Circuits and GraphicsView
    By RY in forum Newbie
    Replies: 10
    Last Post: 1st October 2008, 05:12
  3. QT GraphicsView Help
    By mistertoony in forum Qt Programming
    Replies: 15
    Last Post: 15th February 2007, 04:17
  4. wait copy picture then show it
    By raphaelf in forum Newbie
    Replies: 6
    Last Post: 5th November 2006, 12:09
  5. QPixmap/QImage How to enlarge a picture
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2006, 06:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.