The title of the question is not very clear, I'll try to explain better. I'm using Qt and the windows I'm working with derive (directly or indirectly) from QWidget. My windows has to be treat uniformly, so that I can take a pointer to the currently active window and invoke methods on it not knowing the actual window class, for this reason all my windows derive from another class, let's call it "myScreen". So far so good.
Now I would like to handle in "myScreen" also the hiding and showing of a window, so that it's uniform. For example passing from a window to another may imply calling "hide()" on the current window and "show()" on the new window. Since "myScreen" and QWidget don't have any relationship, I must use dynamic_cast to cast the windows that I know derive from "myScreen" and "QWidget" inside "myScreen" methods, in order to call the functions of QWidget just mentioned.
I know that probably the best way could be having "myScreen" derive from QWidget and all of other windows derive from that, but my goal is to change as little as possible the existing code. I also tried using virtual inheritance, but this approach can't work because of the files generated automatically by the moc (see this link Cannot convert from pointer to base class to pointer to derived class).
By now I managed to ensure that a constructor of "myScreen" is called by a class that derives from QWidget:
struct isQWidget{
class QWidgetType{
QWidgetType(){}
friend struct isQWidget;
};
template<class T>
static QWidgetType isQwidgetType(const T&){
static_assert (std::is_base_of<QWidget, T>::value, "Error: not a QWidget");
return QWidgetType();
}
};
Constructor declaration:
myScreen(myScreen& parentScreen, isQWidget::QWidgetType);
Constructor definition:
sigin::sigin(QWidget *parent, myScreen& si) :
QWidget(parent),
myScreen(si, isQWidget::isQwidgetType(*this)),
ui(new Ui::sigin)
{
ui->setupUi(this);
}
I would like to treat a pointer to myScreen as a pointer to QWidget inside "myScreen" compile-time, since I know I will always use something that derives from QWidget.
Any idea on how I can manage this problem?