PDA

View Full Version : Simplest example for QGraphicsPixmapItem



sincnarf
28th June 2007, 12:03
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)


#include <QGraphicsEllipseItem>
#include <QGraphicsScene>
#include <QGraphicsView>

int main( int argc, char **argv )
{
QApplication app(argc, argv);

QGraphicsScene scene;
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

QGraphicsEllipseItem *item = new QGraphicsEllipseItem( 0, &scene );
item->setRect( -50.0, -50.0, 100.0, 100.0 );

QGraphicsView view( &scene );
view.setRenderHints( QPainter::Antialiasing );
view.show();

return app.exec();

wysota
28th June 2007, 12:28
Substitute lines 12 and 13 with:

QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("yourpixmapfilehere.png"), &scene);

spud
28th June 2007, 12:28
How about

#include <QtGui>

int main( int argc, char **argv )
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap pixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-32.png");

for (int i=0;i<50;i++){
QGraphicsPixmapItem* item = scene.addPixmap(pixmap);
item->moveBy(qrand()%200-100, qrand()%200-100);
item->rotate(qrand()%360);
item->setFlag(QGraphicsItem::ItemIsMovable);
}

view.setRenderHints( QPainter::SmoothPixmapTransform );
view.show();
return app.exec();
}
or if you're going for brevity

#include <QtGui>

int main( int argc, char **argv )
{
QApplication app(argc, argv);
QGraphicsView view(new QGraphicsScene );
view.scene()->addPixmap(QPixmap(":/trolltech/styles/commonstyle/images/standardbutton-help-32.png"));
view.show();
return app.exec();
}

sincnarf
28th June 2007, 13:10
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.

bootchk
20th September 2012, 16:06
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.

d_stranz
20th September 2012, 17:30
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.

bootchk
21st September 2012, 14:55
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.

wysota
21st September 2012, 15:24
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?