PDA

View Full Version : Acces parent object



Nightfox
16th August 2009, 13:46
Hi
I have two classes that both are sub-classed from Qt objects. The first is myMainWindow that inherits from QMainWindow and the second is myWidget that inherits from QWidget.

In myMainWindow I have defined a model (QAbstractTableModel) that I want to access from myWidget. So what I do is this

In the myMainWindow header I define the class
(mymainwindow.h)
class myMainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QAbstractTableModel *model;
myWidget *mywidget;
}

In the implementation file I initiate the objects

(mymainwindow.cpp)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
mywidget = new myWidget(this) ;
}

The problem is when I want to access the model from myMainWindow through myWidget
(mywidget.cpp)
myWidget:: myWidget (QWidget *parent) : QWidget(parent)
{
QAbstractItemModel *model =parent->model;
}

Is it not possible to access the model object through the parent? I’ve been looking for an example the shows this but so far no luck.

Any good suggestions out there? Thanks!

faldzip
16th August 2009, 15:03
"parent" in your case is the pointer to QWidget and QWidget does not have "model" member. You have to cast it to your class. Proper way to do so is to use qobject_cast like this:


myMainWindow *mmw = qobject_cast<myMainWindow *>(parent);
QAbstractItemModel *model = 0;
// now check if mmw is not NULL, because if parent would be pointer to widget different than
// your myMainWindow, qobject_cast returns NULL
if (mmw) {
model = mmw->model;
}

P.S. And use CODE tags in your posts.

Nightfox
16th August 2009, 16:39
Hi faldżip

Thanks for the reply.

I tried your code out and here's the wierd part. It works when I paste it into the contructor of myWidget, but if I call it from one of myWidget functions it doesn't work. I use
this->parent instead of parent in
myMainWindow *mmw = qobject_cast<myMainWindow *>(parent);
In fact both
qobject_cast< myMainWindow *>((this)->parent())
and
((myMainWindow *)this->parent())
works in the contructor of myWidget both neither work in the functions of myWidget. What's going on?

faldzip
16th August 2009, 18:54
can you show us your whole project/files or prepare minimal compilable example reproducing the problem?

Nightfox
18th August 2009, 19:24
Hi faldżip

I hope it's clear from these files what I'm trying to do. I've pastet your code into JournalPanel.cpp and used qobject_cast like you suggested.

Basically what I'm trying to do is to access MainWindow from JournalPanel knowing that it's the parent. This is posible from the contructor but if I'm trying to access it in another function in the class the program crashes.

The JournalPanel is a subclassed widget created in mainwindow.h with (this) as parent. TransactionPanel is created the same way.

The whole idea is to access JournalPanel from TransactionPanel through MainWindow!

Is there an example that shows this? I've gone through alot of Qt documentation but so far no example. Any ideas?

Thanks again!

spirit
19th August 2009, 07:06
did you try to compile these sources? it is not possible! a lot of errors. reupload your sources without errors and we'll help you.

spirit
19th August 2009, 07:08
btw, you can't do this


jourmodel = new QAbstractItemModel(this);

since QAbstractItemModel is abstract class.

spirit
19th August 2009, 07:11
add #include "mainwindow.h" in journalpanel.cpp and this line


mw = qobject_cast<MainWindow *>(this->parent());

will be compiled.

faldzip
19th August 2009, 08:56
or you can also try QWidget::parentWidget()