casting complex widget pointers
Hello,
I've got a class facading two classes, each with its own UI. I don't know at compile time which of these UIs will be shown.
How can I make a function that returns a pointer to on of the UIs:
Code:
{
if (suchAndSuc)
return new class1UI;
else
return class2UI;
}
or is this the wrong approach?
I'm trying to keep everything as modular as possible - each class should bring its own UI with it - but I don't know which classes will be used. So how do I build a form with widgets without knowing their type?
Could just make sure that all the UIs have a common type as parent and pass this as a pointer? i.e. CommonUI* widget(){return new class1UI;}
thanks
K
Re: casting complex widget pointers
If you'll just be passing QWidget pointers, you should be safe (that's what Designer does). Then you can use qobject_cast to cast the result to more complex types if you need them.
Re: casting complex widget pointers
well, I've got something like:
Code:
{
private:
//some signals and slots
}
{
//...
}
//facade implementation
{
if(condition)
//need to pass pointer to class UI_one
return (QWidget*)new UI_one;
//is cast like this ok? else
//need to pass pointer to class UI_two
}
//building the user interface
CompleteUI::show()
{
QWidget* someWidget
= facade
->showUI
() someWidget ->show();//shows on its own
//or
someLayout->addWidget(someWidget );//how it as part of something else
}
is that what you mean? - can I down-cast for the pass and still use them properly?
thanks
K
Re: casting complex widget pointers
ok, I've done it, and it works fine.
I didn't find it obvious that casting a a pointer to something derived from a widget to a widget pointer would work.
thanks for the help,
K
Re: casting complex widget pointers
You don't need those casts either.
Code:
{
if(condition)
//need to pass pointer to class UI_one
return new UI_one;
else
//need to pass pointer to class UI_two
return new UI_two;
}
Also make sure you have a guard so that you don't create new objects everytime showUI is called (unless that is what you intended).