Signal Slot connection from different classes
-
Hello people,
I have two different classes, lets say Class_A and Class_B, where:
@
Class_A::Class_A( Class_B &ref, QWidget *a_parent )
: QDockWidget( "DOCKW", a_parent )
, ref2ClassB( ref )
{
connect( ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
}void Class_A::slot_Do_Some(int value1, int value2)
{
//.. do work
}
//EOF
@and in its header:
@
class Class_A
: public QDockWidget
, public Ui_DockWidget
{
Q_OBJECT
public:
Class_A( Class_B &ref2b, QWidget *parent);private slots:
void slot_Do_Some(int, int);
private: Class_B &ref2ClassB; ///< Reference to Class_B.
};
#endif
//EOF
@As you can see in Class_B:
@
class Class_B
{
private:
void aMethod();signals:
void trigger_Do_Some(int, int);
};
#endif
@and its source:
@
void Class_B::aMethod( void )
{
// blabla
int t = 32;
int r = 45;
emit trigger_Do_Some(t, r);
}
// EOF
@
Class_A is parent and contains a reference to Class_B. My intention is following: When I call Class_B::aMethod() SIGNAL trigger_Do_Some(int, int) is being emitted.
And in the parent class this signal is connected to its slot. Unfortunately I get a error:
@
error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'Class_B' to 'const QObject *'
@Does anybody have a solution for me? Thanks in advise
Huck
-
You need that Class_B inherited QObject or derived from it, then add macro Q_OBJECT:
@class Class_B: public QObject
{
Q_OBJECT
private:
void aMethod();signals:
void trigger_Do_Some(int, int);
};@
and in "connect":http://doc.qt.nokia.com/latest/qobject.html#connect use pointers. -
Not really, I just added Q_OBJECT, but I have the same error.
class Class_B : public QThread, public QObject, also the same error.Then I added via Singleton:
@
const Class_B * pCB = ref2ClassB.GetInstance();
connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
@Now I can compile, but therefore LNK2001 and LNK2019 link errors.
-
Ok, removed.
@
const Class_B * pCB = ref2ClassB.GetInstance();
/// error C2594: 'argument' : ambiguous conversions from 'const Class_B *' to 'const QObject *'
connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
/// error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'Class_B' to 'const QObject *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
connect( ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
@ -
[quote]
All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.
[/quote] from "here":http://doc.qt.nokia.com/4.7/signalsandslots.htmlconst Class_B * pCB = ref2ClassB.GetInstance();
// here the Class_B must inherit from QOBject
connect( pCB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );// here you need the pointer
connect( &ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) ); -
[quote author="rokemoon" date="1313050079"]
// here you need the pointer
connect( &ref2ClassB, SIGNAL(trigger_Do_Some(int, int)), this, SLOT( slot_Do_Some(int, int)) );
[/quote]This way I get an error C2594: 'argument' : ambiguous conversions from 'Class_B *' to 'const QObject *'
-
Latest Class_B:
@
#include <QtCore/QThread>template class SWITCHEXPORT QMap< int, QString >;
class Class_A;
class QModelIndex;class SWITCHEXPORT Class_B : public QThread {
Q_OBJECTpublic:
/// @brief Constructor
Class_B( void );void aMethod();
signal:
void trigger_Do_Some(int, int);private:
QMap< int, QString > categoryNameByID;
}
@ -
There is a @QThread::start();@ in the source .cpp .
So when I inherit from both @clas.s Class_B : public QThread, public QObject@ and I am able to compile it.
But now I have linking errors:
@
3>Linking...
3> Creating library C:\MyProject\Debug\MyProject.lib and object C:\MyProject\Debug\MyProject.exp
3>Class_B.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
3>Class_A.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
3>Class_3.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const Class_B::staticMetaObject" (?staticMetaObject@Class_B@@2UQMetaObject@@B)
3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Class_B::metaObject(void)const " (?metaObject@Class_B@@UBEPBUQMetaObject@@XZ)
3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall Class_B::qt_metacast(char const *)" (?qt_metacast@Class_B@@UAEPAXPBD@Z)
3>Class_B.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall Class_B::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Class_B@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
3>Class_B.obj : error LNK2019: unresolved external symbol "protected: void __thiscall Class_B::trigger_Do_Some(long,long)" (?trigger_Do_Some@Class_B@@IAEXJJ@Z) referenced in function "public: virtual void __thiscall Class_B::HandleMessage(void * const)" (?HandleMessage@Class_B@@UAEXQAX@Z)
3>C:\MyProject\Debug\MyProject.dll : fatal error LNK1120: 5 unresolved externals
@ -
When you have added Q_OBJECT to Class_B.h recently, then assure yourself that moc_ClassB.cxx has been generated as well.
If that is the case, this moc-file should be listed in the .pro or .vcpro file as Source
e.g.:
@
<Files>
<Filter Name="Source Files">
<File RelativePath="C:\path\moc_Class_B.cxx">
</File>
<!-- ....etc...-->
@ -
main.cpp :
@
#include <QtGui>
#include <converter.h>int main(int argc,char *argv[])
{
QApplication app(argc,argv);QGraphicsScene scene(0,0,500,300); QGraphicsView view(&scene); QLabel *l = new QLabel(); QSlider *s = new QSlider(); l->setText("Move the slider handle"); s->setOrientation(Qt::Horizontal); l->setGeometry(100,100,150,50); s->setGeometry(100,200,150,50); Converter *ms = new Converter(s,l); scene.addWidget(l); scene.addWidget(s); view.show(); return app.exec();
}@
converter.cpp :
@
#include "converter.h"Converter::Converter(QSlider *s, QLabel *l)
{
connect(s,SIGNAL(valueChanged(int)),this,SLOT(Changer(int)));
connect(this,SIGNAL(Changer(QString)),l,SLOT(setText(QString)));
}void Converter::Changer(int x)
{
Changer(QString::number(x));
}
@converter.h :
@
#ifndef CONVERTER_H
#define CONVERTER_H#include <QtGui>
class Converter : public QObject
{
Q_OBJECTpublic:
Converter(QSlider*,QLabel*);protected slots:
void Changer(int);signals:
void Changer(QString);};
#endif // CONVERTER_H
@[Edit: Please use @ tags around code. I have added them here. -- mlong]
-
Use Q_PROPERTY for passing arguments in Class-B