Hello Bo,
thanks for your answer.
Of course in reality the signal parameter is meant to be an event object. I have a class hierarchy of special event objects:
class Event
{
public:
enum EventType
{
ET_Message,
ET_Item,
ET_Action,
[...]
};
Event(EventType t);
private:
EventType mEventType;
};
class MessageEvent : public Event
{
public:
MessageEvent(Event::EventType t = Event::Message);
return mMessage;
}
private:
};
class ItemEvent : public Event
{
public:
ItemEvent(Item i, Event::EventType t = Event::Item);
Item item() const {
return mItem;
}
private:
Item mItem;
};
class Event
{
public:
enum EventType
{
ET_Message,
ET_Item,
ET_Action,
[...]
};
Event(EventType t);
private:
EventType mEventType;
};
class MessageEvent : public Event
{
public:
MessageEvent(Event::EventType t = Event::Message);
QString message() const {
return mMessage;
}
private:
QString mMessage;
};
class ItemEvent : public Event
{
public:
ItemEvent(Item i, Event::EventType t = Event::Item);
Item item() const {
return mItem;
}
private:
Item mItem;
};
To copy to clipboard, switch view to plain text mode
In my general state class I have a general signal to emit all types of events, any type of event can be assigned to any state:
{
Q_OBJECT
public:
void addEvent(KeyEvent k, Event* e);
signals:
void event(Event*);
public slots:
void handleEvent(KeyEvent k);
private:
QHash<KeyEvent, Event*> mEventTable;
};
void State::handleEvent(KeyEvent k)
{
// emit event for this key event
if (mEventTable.contains(k))
emit event(mEventTable.value(k));
}
class State : public QObject
{
Q_OBJECT
public:
State(QObject* parent = 0);
void addEvent(KeyEvent k, Event* e);
signals:
void event(Event*);
public slots:
void handleEvent(KeyEvent k);
private:
QHash<KeyEvent, Event*> mEventTable;
};
void State::handleEvent(KeyEvent k)
{
// emit event for this key event
if (mEventTable.contains(k))
emit event(mEventTable.value(k));
}
To copy to clipboard, switch view to plain text mode
Events are registered with states at creation:
class MyOtherClass
{
void initStates()
{
State* s1 = new State();
s1->addEvent(KE_CursorDown, new ItemEvent(item1, Event::ET_Item));
[...]
register all state signals:
connect(s1, SIGNAL(event(Event*)), this, SLOT(onEvent(Event*)));
[...]
}
};
class MyOtherClass
{
void initStates()
{
State* s1 = new State();
s1->addEvent(KE_CursorDown, new ItemEvent(item1, Event::ET_Item));
[...]
register all state signals:
connect(s1, SIGNAL(event(Event*)), this, SLOT(onEvent(Event*)));
[...]
}
};
To copy to clipboard, switch view to plain text mode
So there are "polymorphic" events, so I have to use pointers I think. I want a general event signal for all sorts of events.
Thanks for any suggestions
Bookmarks