Design of custom dynamic interface
-
hello.
I have a interface system that fills dinamically a container layout with a series of widgets from the pression on an entity that can be a different shape object with his class(triangle , rectangle ecc...).
Each click event of each widget must be connected to a only slot runtime.
Only a slot because i don't know how many widget(and relative functionality)I have because the rectangle entity for example has 3 events with relative 3 widgets with icon and text different from the the circle entity that has 2 events(and two widget) ecc...I wish implement a connect function that create the signal / slot connection in each widget class.
the problem are:
1)how i can create a generic slot for connect each signal of each widget click event that manage the "sender" the widget that emit the event?
i think sometings like:
[code]
class widgetBase: public widget
{
public:
virtual void connect(object* pReceiver);
}class customWidget : widgetBase
{
void connect(object* pReceiver){
connect(this,SIGNAL(click()), pReceiver,SLOT(doWidgetClick(this)))}
}
class shape
{
public:
virtual long GetWidgetCount();
virtual widgetBase GetWidgetAt(int idx);
virtual AddWidgetBase AddWidgetAt(widgetBase * pW);
virtual RemoveWidgetBase RemoveWidgetAt(int idx);
protected:
Vector<widgetBase*>* m_pVectorWidget;}
class circle : public Shape
{
long GetWidgetCount();
widgetBase GetWidgetAt(int idx);
AddWidgetBase AddWidget(widgetBase * pW); // i insert 2 widget
RemoveWidgetBase RemoveWidgetAt(int idx);
};
class rectangle : public Shape
{
long GetWidgetCount();
widgetBase GetWidgetAt(int idx);
AddWidgetBase AddWidget(widgetBase * pW); // i insert 5 widget
RemoveWidgetBase RemoveWidgetAt(int idx);
};class MainDialog{
void selectShape(Shape* pShape)
{
layout->clear();
for(int i = 0; i < pShape->GetWidgetCount; i++)
{
widgetBase* pw = pShape->m_GetWidgetAt(i);
pw->connect(this);
layout->addWidget(pw, this);
}
}
slot:
void doWidgetClick(customWidget * pSender)
{
//now i have the widget that is clicked pSender!!!!
}
[/code]may be is a good design?
thanks for advice.
by -
I'm not quite sure about your requirements, but QWidget (more specifically QObject) has a sender() method, which allows for retrieving the emitting object within a slot.
@
void doWidgetClick()
{
CustomWidget pSender = qobject_cast<CustomWidget>(sender());
}
@
For the "correct" solution design-wise you should consider using a "QSignalMapper":http://qt-project.org/doc/qt-4.8/qsignalmapper.html.As to your code:
- You cannot pass values using the connect() statement.
@
// WRONG, this is a value, not a type
connect(this, SIGNAL(click()), pReceiver, SLOT(doWidgetClick(this)));
idget*)));
@ - Slots can only have less, not more arguments (excluding signal default arguments).
@
// WRONG, slot has more arguments than signal
// RIGHT, only if click signal has at least one default argument
connect(this, SIGNAL(click()), pReceiver, SLOT(doWidgetClick(QWidget*)));
@
- You cannot pass values using the connect() statement.