I agree.
You want to avoid type comparison, but you can't complete this without actually comparing something.
So another way. You can see how, for example, QEvent and its subclasses are implemented - there is a type enumeration for each subclass in QEvent class, then its possible to distinguish between the subtypes, using this enumeration. So for your case:
class ObjectAction {
public:
enum Type{
Example,
Other,
....
}
virtual Type type() const = 0;
...
};
...
class ExampleAction : public ObjectAction {
public:
Type type() const{
return Example;
}
...
private:
};
class OtherAction : public ObjectAction {
public:
Type type() const{
return Other;
}
.....
private:
QHash<QString, int> data;
};
class ObjectAction {
public:
enum Type{
Example,
Other,
....
}
virtual Type type() const = 0;
...
};
...
class ExampleAction : public ObjectAction {
public:
Type type() const{
return Example;
}
...
private:
QString data;
};
class OtherAction : public ObjectAction {
public:
Type type() const{
return Other;
}
.....
private:
QHash<QString, int> data;
};
To copy to clipboard, switch view to plain text mode
etc. (actually, its slightly different in QEvent, to allow user-defined events, but its not important right now)
Then you can implement ActionEditors without modifying the Action impl.:
#include "ObjectAction.h"
class ActionEditorCreator{
QWidget * createEditor
( ObjectAction
* action
){ switch(action->type()){
case ObjectAction::Example :
return new StringEditor(action);
case ObjectAction::Other :
return new HashEditor(action);
}
}
};
#include "ObjectAction.h"
class ActionEditorCreator{
QWidget * createEditor( ObjectAction * action ){
switch(action->type()){
case ObjectAction::Example :
return new StringEditor(action);
case ObjectAction::Other :
return new HashEditor(action);
}
}
};
To copy to clipboard, switch view to plain text mode
Does it suits your needs better ? It solves 1) I guess.
As for 2), what do you mean by " QComboBox with action types and setting the current type there", maybe I don't see the whole picture.
Bookmarks