PDA

View Full Version : loadPixmap returns true but does not display/show on-screen



Halordain
10th February 2011, 09:36
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:



MyViewer::MyViewer(QGraphicsScene *scene)
{

QGraphicsGridLayout *layout = new QGraphicsGridLayout;
img_board.resize(8);
for (int i = 0; i < 8; ++i)
img_board[i].resize(8);
for (int j = 0; j < 8; ++j) {
for (int i = 0; i < 8; ++i) {
int index = 8*i + j;
char buffer[200];
QString qindex = itoa(index, buffer, 10);
QString filename(qApp->applicationDirPath() + "/img/");
filename += qindex;
filename += ".png";

QPixmap qimg;
if (qimg.load(filename))
qWarning("Failed to load target image");

img_board[j][i] = new QLabel;
img_board[j][i]->setPixmap(qimg);
QGraphicsProxyWidget* pixwidget = scene->addWidget(img_board[j][i]);
layout->addItem(pixwidget, i, j + 1);
}
}
QGraphicsWidget* form = new QGraphicsWidget;
form->setLayout(layout);
scene->addItem(form);
setWindowTitle(tr("TESTING"));
resize(800, 600);
form->show();
}

.....


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene* scene = new QGraphicsScene;
MyViewer window(scene);
window.show();
return app.exec();
}


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!

high_flyer
10th February 2011, 09:52
From QPixmap doc:

Returns true if the pixmap was successfully loaded; otherwise returns false.



QPixmap qimg;
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.
qWarning("Failed to load target image");



you should also test against isNull(), to be on the safe side.

Halordain
10th February 2011, 10:44
Thank you! I changed it, changed my pathname, and got it to build without any warning messages....... but nothing shows up.



QString qindex = itoa(index, buffer, 10);
QString filename("img/");
filename += qindex;
filename += ".png";

if (!qimg.load(filename))
qWarning("Failed to load target image");
if (qimg.isNull())
qWarning("Failed to load target image");

img_board[j][i] = new QLabel;
img_board[j][i]->setPixmap(qimg);

if (img_board[j][i] == NULL)
qWarning("Failed to load target image");




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




vector<vector<QLabel*> > img_board;



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

high_flyer
10th February 2011, 11:07
Try calling setVisible() on your proxy widgets.