Passing QImage to a function



  • Hi,
    I need to write a function that accept QImage as an input. I declared QImage pointer as
    @QImage *image;@
    And create a new image and set this pointer as
    @QImage img("path to image file");
    image = &img;
    @
    And I have a function like this
    @
    somefunction(QImage *image)
    {
    ui->imgLabel->setPixmap(QPixmap::fromImage(*image)); // This line gives an arithmetic exception why?
    }
    @

    It compiles without any error, but get an arithmetic exception at ui->imgLabel->setPixmap(QPixmap::fromImage(*image));

    Also please let me know the difference between
    @somefunction(QImage *image)@ and @somefunction(QImage &image)@

    Thanking you,
    Ras


  • Lifetime Qt Champion

    At first sight, your img goes out of scope, so it get destroyed.
    You probably also have a warning saying that you are using a pointer to a local variable.

    someFunction should use a const QImage& image (especially since you will set your QImage on a label)

    QImage * is a pointer to a QImage
    QImage & is a reference to a QImage

    Have look at http://www.learncpp.com/cpp-tutorial/612-references-vs-pointers-and-member-selection/

    Hope it helps


Log in to reply
 

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