PDA

View Full Version : how to get the pointer to parent widget/object?



rambo83
26th November 2009, 13:20
Hello,

I would like to call a member function of my mainFrame class from my other Plot class. To do so, I need a pointer to mainFrame class within Plot class. Is there some call like : getParent() or something like this?

In constructor of my Plot class, I already pass the pointer of mainFrame class, so how can I get it then?

Plot::Plot(QWidget *parent):
QwtPlot(parent)
{

Thank you for help.

Vitali

squidge
26th November 2009, 13:33
The parent is already given to your constructor, so just store it somewhere?

Alternatively, you can use parentWidget()

rambo83
26th November 2009, 13:43
It does not work, if I use "parentWidget()->updateChromosome()". The error says:
plot.cpp:453: error: 'class QWidget' has no member named 'updateChromosome'

my mainFrame looks like this:

mainframe.h:

#include "plot.h"
#include <qwt_plot_picker.h>

using namespace std;

class mainFrame : public QMainWindow
{
Q_OBJECT

public:
mainFrame(QWidget *parent = 0);

...


mainframe.cpp:

mainFrame::mainFrame(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
...
d_plot = new Plot(this);
layout_plot->addWidget(d_plot);

So how to get the pointer to the mainFrame class?

My idea was to include mainframe.h into plot.h and declare there

mainFrame *_mf;
and then store the passed pointer to it, but I have already includede plot.h into mainframe.h, so there will be conflict!

Help me please.

Lykurg
26th November 2009, 14:03
You just have to cast the pointer to your class.

rambo83
26th November 2009, 14:12
In this case it is not possible, because I have already included the definition of class "Plot" into the header file of class "mainFrame", so that I cannot include the definition of class "mainFrame" into the header or cpp file of class "Plot"

If I however do so (#inlcude "mainframe.h" in plot.cpp) and then:

mainFrame *mf = dynamic_cast<mainFrame*>(parentWidget());
mf->updateChromosome( selected_Points[i]->value(),index);

then I get this error:
plot.h:11: error: redefinition of 'class Plot'
plot.h:12: error: previous definition of 'class Plot'

So I must somehow trick it around, but how?

Lykurg
26th November 2009, 14:17
then I get this error:
plot.h:11: error: redefinition of 'class Plot'
plot.h:12: error: previous definition of 'class Plot'

So I must somehow trick it around, but how?

Then is your header file wrong! use something like:

#ifndef PLOT_H
#define PLOT_H

class Plot : ...

#endif

Lykurg
26th November 2009, 14:20
In this case it is not possible, because I have already included the definition of class "Plot" into the header file of class "mainFrame", so that I cannot include the definition of class "mainFrame" into the header or cpp file of class "Plot"

You can use a forward declaration!



class mainFrame;

class Plot
{
public:
Plot(mainFrame *mf = 0);
//...
};

rambo83
26th November 2009, 14:29
Yes, you are right, Lykurg!

I have just found something about forward declaration in Google and used it in my code and it works now! Thank you very much.

best regards,

Vitali