Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing QImage to a function
Forum Updated to NodeBB v4.3 + New Features

Passing QImage to a function

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ras123
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved