Solved Return custom widget from QStackedWidget
-
I am trying to access a widget that inherits QWidget which is added to my QStackedWidget.
I have for example a class "PageTwo" that inherits QWidget. This "PageTwo" is added to my QStackedWidget. I call get function in a unrelated class that return the QStackedWidget object, which I then use roughly "Object->getStackedWidget()->widget(PageTwo)" but it complains that it cannot convert from "QWidget *" to "PageTwo".
Am I doing this wrong? I need to be able to access the stackedwidget from all pages so that I can call a function on each page just before going to it.
-
From the description it looks like are trying to assign base class pointer to derived class pointer. Hence hitting the issue
-
Hi
You can access "Pages" with
http://doc.qt.io/qt-5/qstackedwidget.html#widget
it returns a base QWidget pointer so you have to cast it to your type.PageTwo *p2=qobject_cast<PageTwo *>( StackedWidget ->widget(0) );
if(p2) { // always check if cast worked. else it be null
use the custom page.
} -
@mrjj Thank you very much!