[Qt-interest] QHash<QString,class>?
Israel Brewster
israel at frontierflying.com
Mon Jan 12 19:02:52 CET 2009
This may be more of a basic c++ question, but as I am developing in
Qt, and using Qt data structures, I figured I'd ask here. Here's the
situation: I have a number of classes, all of which inherit from a
common baseClass (QMainWindow based, if that makes a difference). In
my code I have a pointer to baseClass, and then, depending on the
value of a QString, I instantiate a new instance of one of these child
classes on said pointer. So at the moment my functioning code looks
basically like:
QString winType; // this is set elsewhere in the code, depending on
what the user clicks, etc.
baseClass * window;
if(winType==winA)
{
window=new winA;
}
else if(winType==winB)
{
window=new winB;
}
else if....
...
and so on and so forth for each child class. This works, but is
obviously ugly and doesn't scale well. I only have two at the moment
actually, but this number could grow arbitrarily. Is there a better
way to do this? I was thinking something like:
QHash <QString,class> winTypeHash;
winTypeHash["winA"]=winA;
winTypeHash["winB"]=winB;
//etc....
window=new winTypeHash[winType];
but of course, that's not an option, at least not as I wrote it. So
what might be a good option? Thanks.
-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
More information about the Qt-interest-old
mailing list