4

I have two functions. In one function, i have a QImage and then i want to pass that QImage to another function. Both the function have different Arguments. Please tell me how can i do it?

CMakeLists.txt

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

rosbuild_init()


# Qt #####################################################
find_package(Qt4 REQUIRED)

set( QT_USE_QTGUI TRUE )

include(${QT_USE_FILE})


add_definitions (-DQT_NO_KEYWORDS)

# All source files
set(SELECTION_INTERFACE_SOURCE_CPP
    src/SelectionInterface.cpp)

# All header files that use Qt Keywords (e.g. OBJECT)
set(SELECTION_INTERFACE_MOC_H
    src/SelectionInterface.h

)

# Wrap for MOC
qt4_wrap_ui (SELECTION_INTERFACE_UI_H ${SELECTION_INTERFACE_UI})
qt4_wrap_cpp(SELECTION_INTERFACE_MOC ${SELECTION_INTERFACE_MOC_H})

rosbuild_add_library (selection_interface_lib
    ${SELECTION_INTERFACE_SOURCE_CPP}
    ${SELECTION_INTERFACE_MOC})


#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

rosbuild_add_executable(newtest_node src/SelectionInterface.cpp)
target_link_libraries (newtest_node selection_interface_lib ${QT_LIBRARIES})

Makefile

include $(shell rospack find mk)/cmake.mk

manifest

<package>
  <description brief="newTestCode">

     newTestCode

  </description>
  <author>admin</author>
  <license>BSD</license>
  <review status="unreviewed" notes=""/>
  <url>http://ros.org/wiki/newTestCode</url>
  <depend package="roscpp"/>
  <depend package="sensor_msgs"/>
  <depend package="std_msgs"/>

</package>

SelectionInterface.h

#ifndef SELECTION_INTERFACE_H
#define SELECTION_INTERFACE_H

    #include <QApplication>
    #include <QtGui/QWidget>
    #include <QtGui/QMenu>
    #include <QtGui/QAction>
    #include <QMainWindow>
    #include <QDebug>
    #include <QLabel>
    #include <QGraphicsPixmapItem>

    class Image:public QMainWindow
    {
        Q_OBJECT

        public:

            void getImage();
            void displayImage();
            QImage tempImage;
    };
    #endif 

SelectionInterface.cpp

#include "SelectionInterface.h"

void Image::getImage()
{
    QImage myImage("/home/usr/Pictures/image.jpg");

    qDebug() << myImage.height();

    tempImage= myImage.copy();

}

void Image::displayImage()
{
    QImage finalImage = tempImage;

    qDebug()<<finalImage.height();
}


int main (int argc, char** argv)
{
    QApplication app(argc, argv);

    Image object;
    object.getImage();
    object.displayImage();
    object.show();

    return app.exec();
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
skm
  • 5,015
  • 8
  • 43
  • 104
  • So what exactly is the problem? "I want to pass QImage from one function to another" is way too broad for SO. Above code seems to basically work at passing the image: one method sets QImage to a member variable, and another uses it. It has other problems, but you are not asking about them it seems. What is the problem you need help with? Also read http://sscce.org – hyde Sep 08 '13 at 16:08
  • Comments under another answer are not a good way to update a question... Anyway, from the wild guesses department, looking at current question code, try `this->tempImage` instead of `tempImage` in all method code. I suspect that your real code has another `tempImage` which shadows the member variable... (of course you should not let member variable be shadowed, other variable should have different name, this is just a quick test) – hyde Sep 08 '13 at 16:42

1 Answers1

12

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().

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
László Papp
  • 51,870
  • 39
  • 111
  • 135