PDA

View Full Version : no image displayed on QGraphicsView



sincnarf
28th June 2007, 09:57
Anyone here has experience with QGraphicsPixmapItem? See, I am trying to load an image from a dialog. After selecting an image, the image must be loaded to a QGraphicsView. I do not know how to do this. here's my load function

void MainWindow::open()
{
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}

QGraphicsScene scene;
scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image), 0, &scene);
item->setShapeMode(QGraphicsPixmapItem::BoundingRectShap e);

//graphicsViewRaw is globally declared as QGraphicsView
graphicsViewRaw->setScene(&scene);
graphicsViewRaw->show();
}
}

how do I paint the image to the QGraphicsView? after graphicsViewRaw->show() no image is loaded.

jpn
28th June 2007, 10:39
QGraphicsScene is allocated on stack so it goes out of scope.

fullmetalcoder
28th June 2007, 10:39
Are you sure anything at all is displayed? You instanciate a QGraphicsScene on stack so it gets deleted as soon as the program leaves the scope where it has been created (in this case the open() method...). You'd better allocate it on heap (i.e. using new) or make it a member of your mainwindow class.



QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect( -100.0, -100.0, 200.0, 200.0 );
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image), 0, scene);
item->setShapeMode(QGraphicsPixmapItem::BoundingRectSha pe);
//graphicsViewRaw is globally declared as QGraphicsView
graphicsViewRaw->setScene(scene);
graphicsViewRaw->show();


p.s : next time use put your code within [c o d e] and [/ c o d e] tags (withut spaces... ;) )

sincnarf
28th June 2007, 11:01
I also created a test program like this



#include <QApplication>

#include <QGraphicsPixmapItem>

#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 );
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("qt4-logo.png"), 0, &scene);
item->setShapeMode(QGraphicsPixmapItem::BoundingRectShap e);
item->setPixmap(QPixmap("qt4-logo.png"));

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

view.show();

return app.exec();

}

still, nothing is displayed . here's a screenshot of the test app.
http://img218.imageshack.us/img218/2491/testld1.th.jpg (http://img218.imageshack.us/my.php?image=testld1.jpg)

fullmetalcoder
28th June 2007, 11:11
is the file qt4-logo.png present within the working directory from which your app is called? did you check the position of the item?

sincnarf
28th June 2007, 11:16
yes, the image is with the same folder as the cpp files. thanks again.

sincnarf
28th June 2007, 11:37
I still can't get the image to load. do i need a qrc file for this? But the problem with qrc files is the image paths must be predefined. I need to load images via QFileDialogs.

jpn
28th June 2007, 12:12
How and where do you launch the application? In case you launch it from a debug/release subdir the image cannot be found in the current working directory (http://wiki.qtcentre.org/index.php?title=Current_Working_Directory).

sincnarf
28th June 2007, 13:16
How and where do you launch the application? In case you launch it from a debug/release subdir the image cannot be found in the current working directory (http://wiki.qtcentre.org/index.php?title=Current_Working_Directory).

Thanks. I can't understand where I'll insert the codes in current working directory (http://wiki.qtcentre.org/index.php?title=Current_Working_Directory). How do I use this?

jpn
28th June 2007, 13:33
It is supposed to be placed anywhere after constructing the QApplication object, but you can of course simply launch the application from the directory containing the image too:


C:\MyApp>release\app.exe

instead of


C:\MyApp\release>app.exe