1

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.

demonplus
  • 5,613
  • 12
  • 49
  • 68
Dennis S
  • 315
  • 3
  • 13
  • 1
    What is the definition of `MainWindow`? `QPointer` only functions on classes that are derived from `QObject` – RobbieE Feb 08 '15 at 08:24
  • 1
    Hmmm I think you have an unnecessary cyclic dependency here (I guess MainWindow will own the dialog). What getter do you want to access in dialog? Why don't you pass required data to dialog when it is created? – Michał Walenciak Feb 08 '15 at 08:25
  • Show your `MainWindow` class declaration – Dmitry Sazonov Feb 08 '15 at 11:33
  • @RobbieE updated with declaration – Dennis S Feb 08 '15 at 19:30
  • @SaZ updated with declaration – Dennis S Feb 08 '15 at 19:30
  • @MichałWalenciak MW will be the parent, yes. And so then? Dialog will post uncertain amount of data to mainwindow to store and count. I need info before every posting. Is it bad design to have a mw_ptr ? – Dennis S Feb 08 '15 at 19:33
  • 1
    Ok, so my answer to your main question: you probably miss `#include "mainwindow.h"` in your dialog's `.cpp` file. This is why compiler doesn't know how to cast it. Answering our discussion here, to avoid cyclic dependency, which is usually bad idea, you can: either use `signal/slot` mechanism (dialog will be emitting signals with your data) or introduce an interface class which will be passed to `dialog`. `MainWindow` can implement this interface, but your dialog will depend on interface not on `MainWindow` which breaks cyclic dependancy. – Michał Walenciak Feb 08 '15 at 20:05

1 Answers1

0

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.

Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • note: cannot convert 'mw_ptr' (type 'MainWindow*') to type 'QObject*' ^ – Dennis S Feb 08 '15 at 19:45
  • Q_OBJECT macro didn't make QMainWindow a QObject ? – Dennis S Feb 08 '15 at 19:46
  • 1
    `Q_OBJECT` doesn't make anything a `QObject` :) it is just a macro which adds set of extra functions. However QMainWindow inherites from `QObject`, so it is `QObject` by definition ;). As I wrote in another comment you probably miss some includes. – Michał Walenciak Feb 08 '15 at 20:09
  • @MichałWalenciak Well, probably you are right, I need additional interface. Thank you, I'll mark question as solved. – Dennis S Feb 09 '15 at 07:42
  • @DensiTensy Michal probably meant to include _MainWindow.h_ into _MyDialog.cpp_ – Zlatomir Feb 09 '15 at 07:48