August 10, 2011

endhck endhck
Lab Rat
10 posts

[SOLVED]convert const QImage & to QImage *

 

I want to use this function:

  1. myLabel->setPixmap(QPixmap::fromImage(myImage));

In my header file I created

  1. QImage *myImage;

and, in cpp file I initialized it
  1. myImage = new QImage(900, 900, QImage::Format_RGB32);

It is necessary because the size of image is not always 900×900. The size is taken from a file.

fromImage() function takes a parameter in const QImage & type, but in my program it must be QImage *, as I said above.
I used like this

  1. myLabel->setPixmap(QPixmap::fromImage(&myImage))

this code does not give any build error, but during execution gives access violation error.

How can I use it, or how can convert?

9 replies

August 10, 2011

peppe peppe
Ant Farmer
1028 posts

But why are you using a pointer to a QImage? And that code SHOULD give you an error, since the type passed to fromImage is “QImage **” which of course is not compatible with a “const QImage &”.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

August 10, 2011

Vass Vass
Ant Farmer
743 posts

Are you sure that you initialize myImage pointer strongly before call QPixmap::fromImage?
Also, are you sure myLabel already created?
You can start app under debugger and looking what happened.

 Signature 


Vasiliy

August 10, 2011

Johan Solo Johan Solo
Ant Farmer
134 posts

I think you could read directly the file you want in the QImage using something like this [doc.trolltech.com].

 Signature 

`They did not know it was impossible, so they did it.’
- Mark Twain

August 10, 2011

endhck endhck
Lab Rat
10 posts

because, I created it in header file and initialized in cpp file.

  1. QImage *myImage;
// in header file
  1. myimage = new QImage(900, 900, QImage::Format_RGB32);
// in cpp file

I tried to write

  1. QImage myImage;
instead of
  1. QImage myImage;

but it gives error because of initialization.

Is there any way of using it without pointer?

August 10, 2011

Gerolf Gerolf
Robot Herder
3286 posts
peppe wrote:
But why are you using a pointer to a QImage? And that code SHOULD give you an error, since the type passed to fromImage is “QImage **” which of course is not compatible with a “const QImage &”.

so the code must be then:

  1. myLabel->setPixmap(QPixmap::fromImage(*myImage))

A reference requires an object not a pointer.

  1. class X* p; // is a pointer

To dereference a pointer to an object, you must put a star in front:

  1. class X {...};
  2.  
  3. X* p;
  4. X obj;
  5. obj = *p;
  6. X& ref1 = obj; // correct, reference to obejct
  7. X& ref2 = *p; // correct, reference to dereferenced pointer
  8. X& ref3 = p; // error, as the reference is not a pointer!
  9. X*& ref4 = p; // correct, as the reference references a pointer to X --> modifying ref4 modifies p, not the object behind p!

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 10, 2011

Johan Solo Johan Solo
Ant Farmer
134 posts
endhck wrote:
Is there any way of using it without pointer?

It’s hard to say whether you really need a pointer or not from the lines you gave us… A priori you don’t need one.

 Signature 

`They did not know it was impossible, so they did it.’
- Mark Twain

August 10, 2011

endhck endhck
Lab Rat
10 posts

yes, it is my fault.
I written this part as

  1. myLabel->setPixmap(QPixmap::fromImage(*myImage))

It does not give build error, it gives error during execution “Access violation”

It show me, the error is in that part:

  1. bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
  2. {
  3.     Q_Q(QCoreApplication);
  4.     if (receiver != q) {
  5.         for (int i = 0; i < receiver->d_func()->eventFilters.size(); ++i) { // in this part
  6.             register QObject *obj = receiver->d_func()->eventFilters.at(i);
  7.             if (!obj)
  8.                 continue;
  9.             if (obj->d_func()->threadData != receiver->d_func()->threadData) {
  10.                 qWarning("QCoreApplication: Object event filter cannot be in a different thread.");
  11.                 continue;
  12.             }
  13.             if (obj->eventFilter(receiver, event))
  14.                 return true;
  15.         }
  16.     }
  17.     return false;
  18. }

August 13, 2011

endhck endhck
Lab Rat
10 posts

Thanks guys,

the problem was not about the QImage.
I think it is an initialization problem, firstly I created the label and the image in the constructor,
after I filled the image and called the setPixmap function. It gave me error.
Now, I created the label and the image, and also called setPixmap function in the constructor.
After I filled the image and called setPixmap again. It works correctly.

August 13, 2011

mlong mlong
Robot Herder
1551 posts

Be sure and change the title of the thread to add [Solved]. Thanks!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ I don’t know Qt’s equivalent of gtk_window_present()      Resizing a widget !! ››

You must log in to post a reply. Not a member yet? Register here!