Results 1 to 4 of 4

Thread: loadPixmap returns true but does not display/show on-screen

  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Platforms
    Windows

    Default loadPixmap returns true but does not display/show on-screen

    Hi,

    I extended QGraphicsView and, in its constructor, want to load a few pixmaps to display on-screen. To test that the pixmaps are properly loaded, I use qWarning (see below), and from what I can tell in the watch window locals, the image is being loaded. However, past that point, I don't know how to check the data in QLabel if it isn't showing up properly on screen. QLabels are loaded into QGraphicsGridLayout, which is then setLayout into the top-level QGraphicsObject:

    Qt Code:
    1. MyViewer::MyViewer(QGraphicsScene *scene)
    2. {
    3.  
    4. QGraphicsGridLayout *layout = new QGraphicsGridLayout;
    5. img_board.resize(8);
    6. for (int i = 0; i < 8; ++i)
    7. img_board[i].resize(8);
    8. for (int j = 0; j < 8; ++j) {
    9. for (int i = 0; i < 8; ++i) {
    10. int index = 8*i + j;
    11. char buffer[200];
    12. QString qindex = itoa(index, buffer, 10);
    13. QString filename(qApp->applicationDirPath() + "/img/");
    14. filename += qindex;
    15. filename += ".png";
    16.  
    17. QPixmap qimg;
    18. if (qimg.load(filename))
    19. qWarning("Failed to load target image");
    20.  
    21. img_board[j][i] = new QLabel;
    22. img_board[j][i]->setPixmap(qimg);
    23. QGraphicsProxyWidget* pixwidget = scene->addWidget(img_board[j][i]);
    24. layout->addItem(pixwidget, i, j + 1);
    25. }
    26. }
    27. QGraphicsWidget* form = new QGraphicsWidget;
    28. form->setLayout(layout);
    29. scene->addItem(form);
    30. setWindowTitle(tr("TESTING"));
    31. resize(800, 600);
    32. form->show();
    33. }
    34.  
    35. .....
    36.  
    37.  
    38. int main(int argc, char *argv[])
    39. {
    40. QApplication app(argc, argv);
    41. MyViewer window(scene);
    42. window.show();
    43. return app.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

    The application builds and runs, but the window is just one large white box. I tried digging into img_board as well as layout in the debugger, but the values are a bunch of addresses, with nothing I can use to verify that I properly loaded the pixmap. However, the warnings have stopped, so loads are working, I think.

    Thanks for your help!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: loadPixmap returns true but does not display/show on-screen

    From QPixmap doc:
    Returns true if the pixmap was successfully loaded; otherwise returns false.
    Qt Code:
    1. QPixmap qimg;
    2. if (qimg.load(filename)) //if this warning is NOT printed, it means the image is NOT loaded - you need to add '!' to the if statement.
    3. qWarning("Failed to load target image");
    To copy to clipboard, switch view to plain text mode 

    you should also test against isNull(), to be on the safe side.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    5
    Platforms
    Windows

    Default Re: loadPixmap returns true but does not display/show on-screen

    Thank you! I changed it, changed my pathname, and got it to build without any warning messages....... but nothing shows up.

    Qt Code:
    1. QString qindex = itoa(index, buffer, 10);
    2. QString filename("img/");
    3. filename += qindex;
    4. filename += ".png";
    5.  
    6. if (!qimg.load(filename))
    7. qWarning("Failed to load target image");
    8. if (qimg.isNull())
    9. qWarning("Failed to load target image");
    10.  
    11. img_board[j][i] = new QLabel;
    12. img_board[j][i]->setPixmap(qimg);
    13.  
    14. if (img_board[j][i] == NULL)
    15. qWarning("Failed to load target image");
    To copy to clipboard, switch view to plain text mode 

    ...where, in the .h file, I define...

    Qt Code:
    1. vector<vector<QLabel*> > img_board;
    To copy to clipboard, switch view to plain text mode 

    Compile message is

    Starting C:\Qt\qt\examples\widgets\tetrix-build-desktop\debug\tetrix.exe...
    BLANK WINDOW APPEARS
    I close it...
    C:\Qt\qt\examples\widgets\tetrix-build-desktop\debug\tetrix.exe exited with code 0

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: loadPixmap returns true but does not display/show on-screen

    Try calling setVisible() on your proxy widgets.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Can't see widget, yet isVisible() returns true
    By MattPhillips in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2010, 12:56
  2. Replies: 0
    Last Post: 21st October 2010, 11:46
  3. Replies: 6
    Last Post: 6th September 2010, 13:38
  4. Replies: 2
    Last Post: 2nd October 2009, 15:32
  5. connect() returns true but slot not called
    By OriginalCopy in forum Qt Programming
    Replies: 6
    Last Post: 4th November 2007, 18:54

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.