First of all, it is better that you use the term methods rather than functions, because these are class members and you will find most people calling these methods out there. Namely, these are members of your Image class.
You can use a member variable for it inside the Image class, which is accessible inside the first method as well as second.
class member
#include <QApplication>
#include <QMainWindow>
#include <QImage>
#include <QDebug>
class Image : public QMainWindow
{
Q_OBJECT
public:
void getImage() {
QImage myImage("/home/lpapp/Downloads/android.png");
qDebug() << myImage.height();
tempImage= myImage.copy();
}
void displayImage() {
QImage finalImage = tempImage;
qDebug() << finalImage.height();
}
private:
QImage tempImage;
};
#include "main.moc"
int main (int argc, char** argv)
{
QApplication app(argc, argv);
Image object;
object.getImage();
object.displayImage();
object.show();
return app.exec();
}
This should print out the same height, to make a very quick verification. You can find the command below, on my system, as to how to build and run this code. You need to generate the moc file, and then supply the include paths and libraries for the build and then, finally, run the application.
moc-qt5 -o main.moc main.cpp && g++ -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I/usr/include/qt -fPIC -lQt5Core -lQt5Widgets -lQt5Gui main.cpp && ./a.out
Output:
900
900
Although, depending on your use case, copy-on-write (COW) might not be sufficient enough, and you will want to use a pointer member to avoid the costly operation.
The documentation can be found here for the copy method; it will copy the whole, by default, if you do not specify the sub-area.
You can also use a static variable inside the same file that you set in method 1, and access in method 2. Note, you should define the static, in that case, outside the class to get a different approach. This is probably less frequently used than the class member, so I would prefer that.
You could probably also set the image on an external class, or variable, and then access. This all depends a lot on your particular case. This is broad question.
You could always use a QPainter as well, to draw the "source" into the "destination" with drawImage().