I'm creating a dialog window and want to know, how to pass a pointer to MainWindow
to it ?
Say, I need to access a getter method from MainWindow
in my dialog.
MainWindow
declaration is straight from the wizard:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
In my dialog.h:
QPointer <MainWindow> mainwindow;
In constructor:
MyDialog::MyDialog(MainWindow *mw_ptr, QWidget *parent) :
QDialog(parent),
ui(new Ui::SceneDialog)
..
mainwindow = mw_ptr;
This gives me the error: invalid static_cast from type 'MainWindow*' to type 'QPointer::QObjectType* {aka QObject*}'
And I don't know why.
Use object_cast to cast the pointer into the type of MainWindow and since documentation doesn't say it's safe to construct QPointer with a null pointer, you can do this safer
MainWindow* ptr = qobject_cast<MainWindow*>(mw_ptr);
if(ptr != 0)
mainwindow = ptr;
Or an alternative way is to use signals and slots to communicate between main-window and the dialog.