[Solved] Slot inheritance problem despite Q_Object Macro
-
Hello,
Currently I have a strange problem with slot inheritance. In other forums this problem will talked, too, but it doesn't help me.
At first, I have the abstract superclass (non-essential things I removed):
@class ToyObject {
Q_OBJECT Q_PROPERTY(QString _name READ getName WRITE setName) public: explicit ToyObject(QObject *parent = 0); protected: QString _name; public slots: void setName(QString val) { _name = val; } //....
};@
Then my subclass (overwrites the abstract virtual method, that not showed here):
@class GCharacter : public ToyObject {
Q_OBJECT public: explicit GCharacter(QObject *parent = 0); //....
};@
No I used the polymorphism in an other class called OInspector. In header file I declared following methods:
@
void defaultPreparation(ToyObject *to);void prepare(GCharacter *gCharacter);
@And implements this:
@
void OInspector::prepare(GCharacter *gCharacter){defaultPreparation(gCharacter); //connection to another Slot of GCharacter, that works QSpinBox *spinFram = createSpinner(); connect(spinFram, SIGNAL(valueChanged(int)), gCharacter, SLOT(setNumOfFrames(int)));
}
void OInspector::defaultPreparation(ToyObject *to){
_nameEdit = new QLineEdit(to->getName(), this); connect(_nameEdit, SIGNAL(textChanged(QString)), to, SLOT(setName(QString)));
}
@When I run the application, I get the following warning code:
bq. QObject::connect: No such slot GCharacter::setName(QString) in ...
But GCharacter should inherit the slot from ToyObject, or not? In QtCreator I can call the inherited method without problems.
Thanks for help.
-
Hi,
[quote]@class ToyObject {
...@
...
GCharacter should inherit the slot from ToyObject, or not?[/quote]Yes, it should inherit the slot from ToyObject, if you do the following:Make sure ToyObject publicly inherits QObject
Make sure you declare both ToyObject and GCharacter in a .h file, not a .cpp file
Run qmake
Recompile your project
-
I just wanted to iterate what JSKH wrote, is this public inheritance? i.e. your class headers look like
@
class ToyObject : public ISerializable
@and
@
class ISerializable : public QObject
@
? -
-
Yes, it is public inheritance, too. In any case.. checked.
hskoglund: You are a genius! It works! :)
So my conclusion:
If you want to work with inherited slots in Qt 5, use the new signal-slot-syntax. Apart from that, I like more the old syntax - the reason follows:
If I connect for example a QComboBox with another object and want to use the signal currentIndexChanged, there are two possible methods (with Integer or QString argument). So the compiler has a problem to decide, which signal is wanted. I have to explicitly tell the compiler the correct functions address:@
connect(combobox,
static_cast<void (QComboBox::*)(int)>
(&QComboBox::currentIndexChanged),
object,
&MyObjectClass::workWithData);
@Here some other references for people who have a similar problem:
"New Signal Slot Syntax":http://qt-project.org/wiki/New_Signal_Slot_Syntax
"New Signal Slot Syntax with QComboBox":http://qt-project.org/forums/viewthread/21513Thank you all for help. :)