Reputation: 416
I have a simple object library that will be used for storing additional data about variables. As I create each object (to be used as variable), I would like to maintain a QList of these objects. Is there a way to automatically connect all of the newly created object's signals to one slot. The slot belongs to an object within my application.
Object Header ezbool.h
class EZBool : public QObject
{
Q_OBJECT
public:
explicit EZBool(QObject *parent = 0);
EZBool(QString boolName, bool initialValue, IOTYPE type);
//Removed Member Functions
signals:
void localEZBool_Created(EZBool *me);
private:
QString name;
bool value;
IOTYPE ioType;
};
Object Constructor ezbool.cpp
EZBool::EZBool(QString boolName, bool initialValue, IOTYPE type)
{
name = boolName;
value = initialValue;
ioType = type;
emit localEZBool_Created(this);
}
Slot Implementation fbtest.cpp
void TestFB::on_localEZBool_Created(EZBool *newVar)
{
qDebug() << "Auto Connect worked??";
//Handle new variable by adding to QList
}
Objects are created in the following manner within fbtest.cpp:
void TestFB::initialize()
{
mpOnCmd = new EZBool("MpOnCmd", false, IO_INPUT);
zeroSpeed = new EZBool("ZeroSpeed", false, IO_INPUT);
}
I have tried matching Qt's auto connect feature, but no luck. The auto connect ability was referenced from the following:
I do not want to manually connect every signal after instantiating each new object. In the following manner:
connect(ezBool, SIGNAL(localEZBool_Created(EZBool*)),
this, SLOT(on_localEZBool_Created(EZBool*)));
Does anyone have any recommendations or suggestions? Thank you!
Upvotes: 2
Views: 2373
Reputation: 21220
To solve objects list auto-population I wouldn't use signal/slot mechanism at all. It actually seems to be an overkill and unnecessary dependency from QObject. Instead, I would propose the following solution that, IMO, much simpler.
Define the list that will store newly created objects. You can read it whenever you want:
static QList<EZBool *> Objects;
Class EZBool
will automatically populate the list with newly created objects:
class EZBool
{
public:
EZBool(QString boolName, bool initialValue, IOTYPE type)
:
name(boolName),
value(initialValue),
ioType(type)
{
Objects.push_back(this);
}
private:
QString name;
bool value;
IOTYPE ioType;
};
This class is responsible for new EZBool objects creation.
class TestFB
{
public:
void initialize()
{
new EZBool("MpOnCmd", false, IO_INPUT);
new EZBool("ZeroSpeed", false, IO_INPUT);
}
};
Upvotes: 0
Reputation: 48196
you can override childEvent in TestFB and then react to it for the ChildAdded
void TestFB::childEvent(QChildEvent * event)
{
if(event.added())
{
EZBool child = qobject_cast<EZBool*>(event.child());
if(child)
{
qDebug() << "Auto Connect worked??";
//Handle new variable by adding to QList
}
}
}
this needs the parent of EZBool to be set to the TestFB
(preferably in the constructor)
mpOnCmd = new EZBool("MpOnCmd", false, IO_INPUT, this);
zeroSpeed = new EZBool("ZeroSpeed", false, IO_INPUT, this);
and add a QObject *parent=0
parameter to the custom constructor and pass it to the super class
EZBool::EZBool(QString boolName, bool initialValue, IOTYPE type, QObject p)
: QObject(p)
{
name = boolName;
value = initialValue;
ioType = type;
}
Upvotes: 1