[SOLVED]convert const QImage & to QImage *



  • I want to use this function:
    @myLabel->setPixmap(QPixmap::fromImage(myImage));@

    In my header file I created @QImage *myImage;@
    and, in cpp file I initialized it @myImage = new QImage(900, 900, QImage::Format_RGB32);@
    It is necessary because the size of image is not always 900x900. 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
    @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?



  • 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 &".



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



  • I think you could read directly the file you want in the QImage using something like "this":http://doc.trolltech.com/4.7/qimage.html#QImage-9.



  • because, I created it in header file and initialized in cpp file.
    @QImage *myImage;@ // in header file
    @myimage = new QImage(900, 900, QImage::Format_RGB32);@ // in cpp file

    I tried to write
    @QImage myImage;@ instead of @QImage myImage;@
    but it gives error because of initialization.

    Is there any way of using it without pointer?



  • [quote author="peppe" date="1312980799"]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 &".[/quote]

    so the code must be then:

    @
    myLabel->setPixmap(QPixmap::fromImage(*myImage))
    @

    A reference requires an object not a pointer.

    @
    class X* p; // is a pointer
    @

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

    @
    class X {...};

    X* p;
    X obj;
    obj = *p;
    X& ref1 = obj; // correct, reference to obejct
    X& ref2 = p; // correct, reference to dereferenced pointer
    X& ref3 = p; // error, as the reference is not a pointer!
    X
    & ref4 = p; // correct, as the reference references a pointer to X --> modifying ref4 modifies p, not the object behind p!
    @



  • [quote author="endhck" date="1312981242"]Is there any way of using it without pointer?[/quote]

    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.



  • yes, it is my fault.
    I written this part as
    @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:
    @bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
    {
    Q_Q(QCoreApplication);
    if (receiver != q) {
    for (int i = 0; i < receiver->d_func()->eventFilters.size(); ++i) { // in this part
    register QObject *obj = receiver->d_func()->eventFilters.at(i);
    if (!obj)
    continue;
    if (obj->d_func()->threadData != receiver->d_func()->threadData) {
    qWarning("QCoreApplication: Object event filter cannot be in a different thread.");
    continue;
    }
    if (obj->eventFilter(receiver, event))
    return true;
    }
    }
    return false;
    }@



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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.