Pass a QImage object between class methods - c++

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();
}

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

Related

Qt and CMake fails with duplicate symbols

I have 3 files in my c++/qt project and I'm using CMake. I'm trying to compile it Here are some code:
CMakeLists contains:
cmake_minimum_required(VERSION 3.8)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH /Users/username/Qt/5.9.2/clang_64/)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
find_package(Qt5Network)
set(SOURCE_FILES main.cpp server.cpp)
add_executable(untitled ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Network)
Main.cpp contains:
#include <iostream>
#include <QCoreApplication>
#include <QtDebug>
#include "server.cpp"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
MyTcpServer server;
return app.exec();
}
and finally server.cpp contains:
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include "server.moc"
class MyTcpServer : public QObject
{
Q_OBJECT
public:
explicit MyTcpServer(QObject *parent = 0);
public slots:
void slotNewConnection();
void slotServerRead();
void slotClientDisconnected();
private:
QTcpServer * mTcpServer;
QTcpSocket * mTcpSocket;
};
MyTcpServer::MyTcpServer(QObject *parent) : QObject(parent)
{
...
}
void MyTcpServer::slotNewConnection()
{
...
}
void MyTcpServer::slotServerRead()
{
...
}
void MyTcpServer::slotClientDisconnected()
{
mTcpSocket->close();
}
I'm trying to compile my project with CMake, and when I run the CMake, I have this problems :
duplicate symbol __ZN11MyTcpServer18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv in:
CMakeFiles/untitled.dir/main.cpp.o
CMakeFiles/untitled.dir/server.cpp.o
...
duplicate symbol __ZN11MyTcpServer16staticMetaObjectE in:
CMakeFiles/untitled.dir/main.cpp.o
CMakeFiles/untitled.dir/server.cpp.o
ld: 13 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Telling me that there is a duplicate symbol.
How to resolve this?
It is better not to use #include for .cpp files. It is good practice to split definition and declaration into different files.
(One exception from this is the private declaration in case of PIMPL pattern.)
In case you want to avoid the split because you have only small pieces of code use a header file and implement your methods within the definition of the class.
In case a library is implemented: Do not install the header file in case your class must not accessible from outside.
The simple fix for your case would be fo#include "server.cpp" from your cpp as mentioned in comments (it is rarely needs to be done and virtually never is a good thing).
And the second thing would be to move the #include "server.moc" line to the end of the cpp file. This is important because this file contains implementation of some member functions injected with Q_OBJECT and the class needs to be defined before implementing member functions outside of its body.

ERROR that looks so main.cpp:(.text.startup+0xd6): undefined reference to `vtable for Counter'?

I have naturally trivial question as I mean: we press button --> counter increases, counter increases --> QLabel's value is renewed. I caught strange error and don't want to do. I'm not dummy in C++ but in QT I am. It's my first and most trivial application in it.
Some answers there (on Stack Overflow) advised to add virtual constructor. It has no effect.
I tried to rewrite signals and slots to new qt5 style but there were another problems, I was too lazy to fix them, was it (rewriting, not laziness :) ) a good way, maybe problem is really with versions?
I just haven't tried to reinstall QT or install Qt4, maybe problem is in it?
about versions:
$ qmake --version
responds:
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu
conn.pro:
TEMPLATE = app
QT += core gui
TARGET = conn
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp
main.cpp:
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QObject>
class Counter : public QObject {
Q_OBJECT
private:
double i_;
public:
virtual ~Counter()
{
}
Counter() : QObject(), i_(0)
{
}
public slots:
void slot_incCounter();
signals:
void goodbye(){}
void counterChanged(double){}
};
void Counter::slot_incCounter() {
emit counterChanged(++i_);
if (i_ == 5) {
emit goodbye();
}
}
int main(int argc, char* argv[]) {
QApplication my_app(argc, argv);
QLabel label1("label i created");
label1.show();
QPushButton button1("press me");
button1.show();
Counter counter1;
QObject::connect(&button1, SIGNAL(clicked()),
&counter1, SLOT(slot_incCounter()));
QObject::connect(&counter1, SIGNAL(counterChanged(double a)),
&label1, SLOT(setNum(double a)));
QObject::connect(&counter1, SIGNAL(goodbye()),
&my_app, SLOT(quit()));
return my_app.exec();
}
Try to run it:
qmake && make && ./conn
So I see in console:
g++ -m64 -Wl,-O1 -o conn main.o -L/usr/X11R6/lib64 -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
main.o: In function `main':
main.cpp:(.text.startup+0xd6): undefined reference to `vtable for Counter'
collect2: error: ld returned 1 exit status
Makefile:144: recipe for target 'conn' failed
make`:` *** [conn] Error 1
What should I do?
Qt uses the meta object compiler (moc) to enable e.g. signal and slots. By default it works perfectly if the Q_OBJECT macro is in a header file. So the easiest would be you put Counter into it's own header/implementation file, rerun qmake and make. (That's by the way good practice...)
If you want to stick with a single main.cpp file you need to tell the moc explicitly that this file contains macros moc needs to parse. You do this with the following line at the very end of main.cpp:
#include "main.moc"
Then also rerun qmake and make.
Please keep in mind that the manually including a moc-include directive is not the best choice. So better split your C++ classes into separate files right from the beginning...
Thank you very much! Your answer was full, useful and making all more obvious.
Solution was:
1. Move class Counter to Counter.h
Since this moment the message about vtable disappeared. Appeared messages that goodbye() and Counter::counterChanged(double) have multiple definition. The first definition was mine in Counter.cpp (WRONG WAY). The second was in moc_Counter.cpp, generated by MOC utility. So:
2. Remove definitions (my empty definitions) of signal functions, because moc makes its own in file moc_Counter.cpp:
// SIGNAL 0
void Counter::goodbye()
{
QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR);
}
// SIGNAL 1
void Counter::counterChanged(double _t1)
{
void *_a[] = { Q_NULLPTR, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
QMetaObject::activate(this, &staticMetaObject, 1, _a);
}
and they cause a problem of multiple definition.
Summing it up, working code:
main.cpp:
#include <QApplication>
#include "Counter.h"
int main(int argc, char* argv[]) {
QApplication my_app(argc, argv);
QLabel label1("1");
label1.show();
QPushButton button1("press me");
button1.show();
Counter counter1;
QObject::connect(&button1, SIGNAL(clicked()),
&counter1, SLOT(slot_incCounter()));
QObject::connect(&counter1, SIGNAL(counterChanged(double)),
&label1, SLOT(setNum(double)));
QObject::connect(&counter1, SIGNAL(goodbye()),
&my_app, SLOT(quit()));
return my_app.exec();
}
void Counter::slot_incCounter() {
emit counterChanged(++i_);
if (i_ == 5) {
emit goodbye();
}
}
Counter.h:
#ifndef COUNTER_H
#define COUNTER_H
#include <QLabel>
#include <QPushButton>
#include <QObject>
class Counter : public QObject {
Q_OBJECT
private:
double i_;
public:
virtual ~Counter()
{
}
Counter() : QObject()
{
}
public slots:
void slot_incCounter();
signals:
void goodbye();
void counterChanged(double);
};
#endif // COUNTER_H
Counter.cpp:
#include "Counter.h"
Thank you, you're great!

Define signals and slots inside main.cpp

I wrote a little program with a my own class within the main.cpp. Here the code:
#include <QApplication>
#include <QPushButton>
#include <QLabel>
class MyWidget : public QWidget {
//Q_OBJECT
public:
MyWidget(QWidget* parent = 0);
QLabel* label;
QString string;
signals:
public slots:
void setTextLabel();
};
void MyWidget::setTextLabel() {
label->setText("Test");
}
MyWidget::MyWidget(QWidget* parent)
: QWidget(parent) {
}
int main(int argc, char** argv) {
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}
it seems work but not "completely". My slot doens't work. I suppose i have to put Q_OBJECT. BUT, doing so, I got a list of errors, like this:
undefined reference to `vtable for MyWidget'
........................................
collect2: error: ld returned 1 exit status
make: *** [mywidget] Error 1
I can I manage that? Where the problem?
Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.
The problem here is that your class is declared in main.cpp: this conflicts with how the moc is working with your code. You should declare your class in a separate header.
More about the moc
Edit: as hyde pointed, an alternative is to include in your cpp the file generated by the moc: Why is important to include “.moc” file at end of a Qt Source code file?
just append the line #include"main.moc" to your cpp source file should be enough.
More information:
Why is important to include ".moc" file at end of a Qt Source code file?

QTimer::singleShot() looks for the specified slot in the given object's parent class, not the object itself

I am fairly new to Qt. I have done some simple modifications to an existing Qt application, but I haven't created any from scratch yet.
I also don't have really much experience with certain aspects of C++ in general (class inheritance etc).
I have created a new Code::Blocks Qt4-based project and modified the template a bit. I have added two files.
Right now the project contains three files: main.cpp, app.h and app.cpp.
This is the content of main.cpp:
#include <QTimer>
#include "app.h"
int main(int argc, char* argv[]) {
TestApp app(argc, argv);
QTimer::singleShot(1000, &app, SLOT(timeout()));
return app.exec();
}
This is what app.h looks like:
#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED
#include <QApplication>
class TestApp: public QApplication {
public:
TestApp(int &argc, char **argv);
public slots:
void timeout();
};
#endif
And this is app.cpp:
#include "app.h"
#include <QDebug>
TestApp::TestApp(int &argc, char **argv): QApplication(argc, argv) {
}
void TestApp::timeout() {
qDebug() << "timeout called";
}
I expected the program to print "timeout called" one second after startup. Unfortunately, this doesn't work. When QTimer::singleShot() is called, the console says:
Object::connect: No such slot QApplication::timeout() in [path to the main.cpp file]
Object::connect: (receiver name: 'QtTests')
I have no idea how to deal with this. Thank you in advance.
You're simply missing the Q_OBJECT macro in your TestApp class:
class TestApp: public QApplication {
Q_OBJECT
public:
...
This is necessary for the whole signal/slot infrastructure to work (and deriving from a class that has this macro is not sufficient).
(Make sure you do a full, clean build after that change - and if you don't use qmake or some other Qt-aware build system, you'll need to run moc yourself.)
For reference, see the QObject docs:
Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
You need to create a moc file, which is created with qmake if you put Q_OBJECT macro in your class.
So, to fix your example, you need your class changed to this :
class TestApp: public QApplication {
Q_OBJECT
public:
TestApp(int &argc, char **argv);
public slots:
void timeout();
};

Undefined reference to vtable... Q_OBJECT macro [duplicate]

This question already has answers here:
Qt Linker Error: "undefined reference to vtable" [duplicate]
(9 answers)
Closed 9 years ago.
When I uncomment the Q_OBJECT macro that I need for signal-slot I get a undefined reference to vtable for MyApp error, but without the macro it compiles perfectly but I can't use signals and slots without it. I think I may be doing something stupid wrong, but please try helping because I realy can't find the problem. O and I know my code is untidy and am working on it.
myapp.h:
#ifndef MYAPP_H
#define MYAPP_H
#include <QApplication>
#include <QEvent>
#include <QObject>
#include <QDebug>
class MyApp : public QApplication
{
public:
MyApp( int argc, char** argv );
protected:
bool eventFilter(QObject *object, QEvent *event);
signals:
void focusG();
void focusL();
};
#endif // MYAPP_H
myapp.cpp:
#include "myapp.h"
MyApp::MyApp(int argc, char **argv): QApplication(argc, argv)
{
installEventFilter(this);
}
bool MyApp::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::ApplicationDeactivate)
{
qDebug() << "Focus lost";
//focusL();
}
if (event->type() == QEvent::ApplicationActivate)
{
qDebug() << "Focus gained";
//focusG();
}
return false;
}
main.cpp:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QObject>
#include <QGraphicsObject>
#include <QTimer>
#include <QVariant>
#include "timecontrol.h"
#include "scorecontrol.h"
#include "Retry.h"
#include <QEvent>
#include "myapp.h"
int main(int argc, char *argv[])
{
//QApplication app(argc, argv);
MyApp app(argc, argv);
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationLockLandscape);
viewer.setMainQmlFile(QLatin1String("qml/Raker/main.qml"));
viewer.showExpanded();
QObject *rootObject = viewer.rootObject();
QTimer *timmer = new QTimer;
timmer->setInterval(1000);
TimeControl *timcon = new TimeControl;
scorecontrol *scorer = new scorecontrol;
Retry *probeer = new Retry;
QObject::connect(timmer, SIGNAL(timeout()), timcon, SLOT(updateTime()));
QObject::connect(timcon, SIGNAL(setTime(QVariant)), rootObject, SLOT(setTime(QVariant)));
QObject::connect(rootObject, SIGNAL(blockClicked(int, int)), scorer, SLOT(checkRight(int, int)));
QObject::connect(scorer, SIGNAL(setScore(QVariant)), rootObject, SLOT(setScore(QVariant)));
QObject::connect(scorer, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(rootObject, SIGNAL(start()), probeer, SLOT(Reetry()));
QObject::connect(probeer, SIGNAL(start()), timmer, SLOT(start()));
QObject::connect(probeer, SIGNAL(stop()), timmer, SLOT(stop()));
QObject::connect(probeer, SIGNAL(start(int)), scorer, SLOT(randomNum(int)));
QObject::connect(probeer, SIGNAL(sReset()), timcon, SLOT(reset()));
QObject::connect(probeer, SIGNAL(tReset()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(timeOut()), scorer, SLOT(reset()));
QObject::connect(timcon, SIGNAL(setState(QVariant)), rootObject, SLOT(setState(QVariant)));
QObject::connect(timcon, SIGNAL(changeFinal()), scorer, SLOT(changeFinal()));
QObject::connect(scorer, SIGNAL(setFinal(QVariant)), rootObject, SLOT(setFinal(QVariant)));
QObject::connect(&app, SIGNAL(focusL()), probeer, SLOT(focusL()));
QObject::connect(&app, SIGNAL(focusG()), probeer, SLOT(focusG()));
return app.exec();
}
BlockToucher.pro:
# Add more folders to ship with the application, here
folder_01.source = qml/Raker
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =
symbian:TARGET.UID3 = 0x2004b49f
# Smart Installer package's UID
# This UID is from the protected range and therefore the package will
# fail to install if self-signed. By default qmake uses the unprotected
# range value if unprotected UID is defined for the application and
# 0x2002CCCF value if protected UID is given to the application
symbian:DEPLOYMENT.installer_header = 0x2002CCCF
# Allow network access on Symbian
symbian {
#TARGET.CAPABILITY += NetworkServices
vendorinfo = "%{\"Gerhard de Clercq\"}" ":\"Gerhard de Clercq\""
ICON = BlockToucher.svg
}
# If your application uses the Qt Mobility libraries, uncomment the following
# lines and add the respective components to the MOBILITY variable.
# CONFIG += mobility
# MOBILITY +=
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
timecontrol.cpp \
scorecontrol.cpp \
Retry.cpp \
myapp.cpp \
myapplication.cpp
# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()
HEADERS += \
timecontrol.h \
scorecontrol.h \
Retry.h \
myapp.h \
myapplication.h
OTHER_FILES += \
qtc_packaging/debian_fremantle/rules \
qtc_packaging/debian_fremantle/README \
qtc_packaging/debian_fremantle/copyright \
qtc_packaging/debian_fremantle/control \
qtc_packaging/debian_fremantle/compat \
qtc_packaging/debian_fremantle/changelog
compat \
qtc_packaging/debian_fremantle/changelog
Q_OBJECT is used by the MOC system to produce the code you need for the signals. My best guess would be that you are not included the MOC generated files in your project (the CPP files which it generates).
How you do this greatly depends on your build system (CMake, QMake, AUtomake, MSVC) but you should refer to the tutorial with QMake first.
That is, the undefined reference to vtable is the error I sometimes get when I forget to update my Cmake files and have a Q_OBJECT.
Also, your ctor for MyApp is wrong, the signature must be:
MyApp( int & argc, char** argv );
The & is important should you ever use command-line parameters.
I had same issue when i decided to add Q_OBJECT into my header file.
First try run qmake manually.
If not resolved, so delete build folder and build project again
If building all of the project is a heavy task, so try following steps:
Add a new temp class with QObject as its base class
Double check your header and source file for any mistake using new temp class
Build your project. It should built successfully
Remove your temp class completely (.h and .cpp file and relative lines on .pro file)
Good luck :)