Results 1 to 11 of 11

Thread: Non-scaling image added to scene

  1. #1
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Non-scaling image added to scene

    I am currently adding a PNG to the scene with addPixmap. It works just find, moving with the scene. However, I do not want the image to scale when the scene gets scaled. Any suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-scaling image added to scene

    Set the ItemIgnoresTransformations flag.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Non-scaling image added to scene

    Ah, how did I miss that? Thank you very much. The next thing that I need to do is to have the image centered on the position instead of painting from the top-left corner while still not scaling. I hope this is something just as simple that I have missed.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-scaling image added to scene

    Most likely you mean QGraphicsView::alignment property (set it to Qt::AlignCenter).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Non-scaling image added to scene

    I was not very clear with my last message, sorry. I am currently doing the following to add the PNG to the scene:
    Qt Code:
    1. QPixmap *p = new QPixmap(":/some.png");
    2. d = m_graphicsScene->addPixmap(*p);
    3. d->setPos(SceneCoords.x(), SceneCoords.y());
    4. d->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
    To copy to clipboard, switch view to plain text mode 
    I would like this PNG centered on the scene coordinates while still not scaling. With the above code, the scene coordinates is the upper-left of the PNG.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-scaling image added to scene

    Oh, I see. Try calling QGraphicsItem::setTransformOriginPoint() pointing to the center of your pixmap (boundingRect().center()). If it doesn't work then you'll have to subclass your item class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Non-scaling image added to scene

    For the fun of it, I added
    Qt Code:
    1. d->setTransformOriginPoint((p->width() * 200), (p->height() * 200));
    To copy to clipboard, switch view to plain text mode 
    between the above lines 4 & 5. I do not see any difference.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-scaling image added to scene

    So subclass the pixmap item class and redefine the bounding rect to point to the center of the image. Also reimplement paint and translate the painter so that the original paint routine thinks it is still in top-left corner.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Non-scaling image added to scene

    I did not find that changing the bounding rect was needed.
    Qt Code:
    1. class MyGraphicsPixmapItem : public QGraphicsPixmapItem
    2. {
    3. public:
    4. MyGraphicsPixmapItem(const QPixmap& pixmap, QGraphicsItem* parent = 0) :
    5. QGraphicsPixmapItem(pixmap, parent) {};
    6. void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    7. {
    8. painter->translate(-(option->rect.width() / 2), -(option->rect.height() / 2));
    9. QGraphicsPixmapItem::paint(painter, option, widget);
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 
    This is all I had to do. Did I miss anything? Thank you for your direction.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Non-scaling image added to scene

    You have to change the bounding rect. Otherwise you'll get artifacts in an area that you paint and that is not inside your bounding rect.

    Qt Code:
    1. QRectF MyGraphicsPixmapItem::boundingRect() const {
    2. QRectF r = QGraphicsPixmapItem::boundingRect();
    3. return r.translated(-r.width()*0.5, -r.height()*.0.5);
    4. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Non-scaling image added to scene

    Excellent. I actually already had that in, but did not notice any difference. I now remember reading about the artifacts, so thank you for reminding me and you assistance!

Similar Threads

  1. Replies: 5
    Last Post: 10th October 2012, 21:05
  2. QCompleter is not appearing on a widget added to a scene
    By Rodrigo.Celler in forum Qt Programming
    Replies: 4
    Last Post: 8th December 2010, 16:11
  3. QWebView and image scaling
    By naresh in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2010, 03:38
  4. Replies: 1
    Last Post: 24th May 2010, 11:57
  5. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45

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.