Results 1 to 10 of 10

Thread: no image displayed on QGraphicsView

  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 no image displayed on QGraphicsView

    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:pen()
    {
    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::BoundingRectSha pe);

    //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.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: no image displayed on QGraphicsView

    QGraphicsScene is allocated on stack so it goes out of scope.
    J-P Nurmi

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

    sincnarf (4th October 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: no image displayed on QGraphicsView

    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.

    Qt Code:
    1. scene->setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    2. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image), 0, scene);
    3. item->setShapeMode(QGraphicsPixmapItem::BoundingRectSha pe);
    4. //graphicsViewRaw is globally declared as QGraphicsView
    5. graphicsViewRaw->setScene(scene);
    6. graphicsViewRaw->show();
    To copy to clipboard, switch view to plain text mode 

    p.s : next time use put your code within [c o d e] and [/ c o d e] tags (withut spaces... )
    Current Qt projects : QCodeEdit, RotiDeCode

  5. The following user says thank you to fullmetalcoder 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: no image displayed on QGraphicsView

    I also created a test program like this

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QGraphicsPixmapItem>
    4.  
    5. #include <QGraphicsScene>
    6.  
    7. #include <QGraphicsView>
    8.  
    9. int main( int argc, char **argv )
    10.  
    11. {
    12.  
    13. QApplication app(argc, argv);
    14.  
    15. scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    16. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap("qt4-logo.png"), 0, &scene);
    17. item->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
    18. item->setPixmap(QPixmap("qt4-logo.png"));
    19.  
    20. scene.addItem(item);
    21. QGraphicsView view( &scene );
    22. view.setRenderHints( QPainter::Antialiasing );
    23.  
    24. view.show();
    25.  
    26. return app.exec();
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    still, nothing is displayed . here's a screenshot of the test app.

  7. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: no image displayed on QGraphicsView

    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?
    Current Qt projects : QCodeEdit, RotiDeCode

  8. The following user says thank you to fullmetalcoder for this useful post:

    sincnarf (4th October 2007)

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

    Default Re: no image displayed on QGraphicsView

    yes, the image is with the same folder as the cpp files. thanks again.

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

    Default Re: no image displayed on QGraphicsView

    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.

  11. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: no image displayed on QGraphicsView

    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.
    J-P Nurmi

  12. The following user says thank you to jpn for this useful post:

    sincnarf (4th October 2007)

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

    Default Re: no image displayed on QGraphicsView

    Quote Originally Posted by jpn View Post
    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.
    Thanks. I can't understand where I'll insert the codes in current working directory. How do I use this?

  14. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: no image displayed on QGraphicsView

    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
    J-P Nurmi

  15. The following user says thank you to jpn for this useful post:

    sincnarf (3rd July 2007)

Similar Threads

  1. mouse tracking on image
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 12th May 2010, 13:06
  2. JPEG Image isn't displayed!?
    By GodOfWar in forum Qt Programming
    Replies: 9
    Last Post: 16th April 2007, 15:01
  3. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01

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.