jdl
jdl

Reputation: 6323

How can a QWidget get the "entire" parent?

I have instantiated a widgetClass in MainWindow.cpp. I want to pass in "this" to this widget as more than just (QWidget* parent) but also (MainWindow* parent). But on the build, the widgetClass is established before the MainWindow and so it errors out.

I want to get to instanceVariables in the MainWindow??

ie:

myWidget(QWidget* parent, MainWindow* parent);

Upvotes: 1

Views: 1707

Answers (2)

Boris Dalstein
Boris Dalstein

Reputation: 7738

The answer proposed by nkint is the cleanest way to do. However, there exist quick hacks if you don't have time or can't change the architecture of your software:

If you know for sure that the parent of MyWidget is a QMainWindow, you can simply do:

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    QMainWindow * main = static_cast<QMainWindow*>(parent);
    std::cout << main->foo(0) << std::endl;
}

If you are not sure, i.e. may or may not actually be a QMainWindow, then you can do:

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    QMainWindow * main = qobject_cast<QMainWindow*>(parent);
    // here, main is null if and only if parent was not a QMainWindow
    if(main)
        std::cout << main->foo(0) << std::endl;
}

qobject_cast is similar to dynamic_cast. More precisely, the documentation says:

The qobject_cast() function behaves similarly to the standard C++ dynamic_cast(), with the advantages that it doesn't require RTTI support and it works across dynamic library boundaries.

qobject_cast() can also be used in conjunction with interfaces; see the Plug & Paint example for details.

Warning: If T isn't declared with the Q_OBJECT macro, this function's return value is undefined.

Upvotes: 1

nkint
nkint

Reputation: 11733

There are some issues on such task.. Maybe the problem is that you have to use Forward Declaration to include one class in one other that include the first ones.. Or maybe the fact the default constructor of QWidget has the first argument as the default parameter.. what is your error exactly?

Anyway, here is a complete example:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "mywidget.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    int foo(int n);
private:
    Ui::MainWindow *ui;

    MyWidget *w;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    w = new MyWidget(this);
    this->setCentralWidget(w);
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::foo(int n)
{
    std::cout << "foo" << std::endl;
    return n+42;
}

mywidget.h

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>

class MainWindow;

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(MainWindow *main, QWidget *parent = 0);
signals:
public slots:
};

#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h"
#include "mainwindow.h"
#include <iostream>

MyWidget::MyWidget(MainWindow *main, QWidget *parent) :
    QWidget(parent)
{
    std::cout << main->foo(0) << std::endl;
}

Does this help?

Upvotes: 1

Related Questions