Derek
Derek

Reputation: 11915

What is wrong with this function call

Behold the code:

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_Indexed8);
ui->viewLabel->setPixmap(QPixmap::fromImage(qi,Qt::AutoColor));

Why does this give me an error? What is it trying to get me to change the qi argument to in the call to fromImage? I am relatively new to C++ and it seems like I am setting this up correctly. It works if I change the line to

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_Indexed8);
    ui->viewLabel->setPixmap(QPixmap::fromImage(qi[0],Qt::AutoColor));

But is that the correct way to do this?

Upvotes: 1

Views: 594

Answers (4)

Grizzly
Grizzly

Reputation: 20191

Generally it might help, if you give the error message in your question. Now from looking at qts online documentation I think your problem is the following:

you are trying to pass qi which is of type QImage* to QPixMap::fromImage, which exepects a const QImage&, so a reference to QImage. Therefore what you have to do is dereference qi:

ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));

This is basically identical to your second version ui->viewLabel->setPixmap(QPixmap::fromImage(qi[0],Qt::AutoColor));, since ptr[i] is defined as *(ptr+i), so ptr[0] means basically *(ptr+0) which is *ptr. However using * for dereferencing here makes it clearer that you dereference a pointer to a single object, instead of an array.

Upvotes: 2

CharlesB
CharlesB

Reputation: 90316

qi is a pointer, and setPixmap looks like waiting for an object, so that's why the first version fails.

Correct way is

QImage *qi = new QImage(fullCharArray, imwidth, imheight, QImage::Format_Indexed8);
ui->viewLabel->setPixmap(QPixmap::fromImage(*qi,Qt::AutoColor));

Upvotes: 0

etarion
etarion

Reputation: 17141

It's because QPixmap::fromImage expects a reference to an image, not a pointer to one.

However, change your qi[0] to *qi, that makes the intention cleaner. (or just refrain from using a pointer and start with QImage qi(fullCharArray ...).

Upvotes: 3

René Nyffenegger
René Nyffenegger

Reputation: 40499

It looks like QPixmap::fromImage takes a QImage as first argument, not a pointer to a QImage.

Upvotes: 0

Related Questions