Results 1 to 8 of 8

Thread: Simplest example for QGraphicsPixmapItem

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Simplest example for QGraphicsPixmapItem

    Hello I really need help. I need the simplest possible example for QGraphicsPixmapItem.

    Maybe simple can be defined this way (but this is for QGraphicsEllipseItem)

    Qt Code:
    1. #include <QGraphicsEllipseItem>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4.  
    5. int main( int argc, char **argv )
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    10.  
    11. QGraphicsEllipseItem *item = new QGraphicsEllipseItem( 0, &scene );
    12. item->setRect( -50.0, -50.0, 100.0, 100.0 );
    13.  
    14. QGraphicsView view( &scene );
    15. view.setRenderHints( QPainter::Antialiasing );
    16. view.show();
    17.  
    18. return app.exec();
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Simplest example for QGraphicsPixmapItem

    Substitute lines 12 and 13 with:
    Qt Code:
    1. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("yourpixmapfilehere.png"), &scene);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    sincnarf (4th October 2007)

  4. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Simplest example for QGraphicsPixmapItem

    How about
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main( int argc, char **argv )
    4. {
    5. QApplication app(argc, argv);
    6. QGraphicsView view(&scene);
    7. QPixmap pixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-32.png");
    8.  
    9. for (int i=0;i<50;i++){
    10. QGraphicsPixmapItem* item = scene.addPixmap(pixmap);
    11. item->moveBy(qrand()%200-100, qrand()%200-100);
    12. item->rotate(qrand()%360);
    13. item->setFlag(QGraphicsItem::ItemIsMovable);
    14. }
    15.  
    16. view.setRenderHints( QPainter::SmoothPixmapTransform );
    17. view.show();
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 
    or if you're going for brevity
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main( int argc, char **argv )
    4. {
    5. QApplication app(argc, argv);
    6. view.scene()->addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-32.png"));
    7. view.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to spud for this useful post:

    sincnarf (4th October 2007)

  6. #4
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simplest example for QGraphicsPixmapItem

    problem solved at last.

    @wysota Thank you very much for all your help. Sorry for sending duplicate stuff to your inbox.

    @spud Thanks too.

    I can't thank all of you enough. Thanks thanks thanks.

  7. #5
    Join Date
    Apr 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simplest example for QGraphicsPixmapItem: exception not thrown for bad filename

    FYI you must supply a valid path to the file. At least in PySide, after translating the example to Python: No exception is thrown if the file path is wrong, and the view displays blank (without an image).

    That seems like a bug. One of the preconditions to QPixmap(filename) constructor should be: the file exists.
    Maybe it is a limitation of the PySide binding: maybe the C++ function returns an error code.

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,017
    Thanks
    280
    Thanked 816 Times in 805 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Simplest example for QGraphicsPixmapItem: exception not thrown for bad filename

    This thread has been rotting in its grave for over 5 years. Why dig it up now?

    The behaviour of the QPixmap( filename ) constructor when the file does not exist (or no plugin is available to render the image in it) is not a bug; it takes a graceful approach to resolving the problem by simply displaying an empty pixmap, rather than blowing up your program for you. C++ constructors cannot return error codes, they can only throw exceptions or set some flag inside the new instance to indicate that the object could not be constructed in a valid internal state. There is no convention for this, and the Qt approach is much more user friendly than throwing an exception.

  9. #7
    Join Date
    Apr 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Simplest example for QGraphicsPixmapItem

    Sorry, you're correct. I didn't read the documentation for QPixmap.

    But then the documentation for QGraphicsPixmapItem() is weak: it could say "If the passed pixmap is null, the QGraphicsPixmapItem will not be visible at any scale." (If thats how it behaves.) I suppose "there exists some scale at which the item is visible" is not an invariant of the QGraphicsItem class.

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

    Default Re: Simplest example for QGraphicsPixmapItem

    Quote Originally Posted by bootchk View Post
    If the passed pixmap is null, the QGraphicsPixmapItem will not be visible at any scale.
    Hmm... isn't that obvious? What else exactly would you expect to see when passing a null pixmap?
    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.


Similar Threads

  1. simplest / best way to fuse several files with QT?
    By Havard in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 10:06
  2. A simplest ListModel... no example :(
    By tomek in forum Newbie
    Replies: 5
    Last Post: 7th January 2006, 00:32

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.