PDA

View Full Version : Can you access the parent widget of a subclass



jshafferman
6th December 2010, 22:51
I am curious if you can access private variables from a parent? In case I am not communicating what I am trying to do, below is an example:



#ifndef MYVIEW_H
#define MYVIEW_H

#include <QTreeView>

class MyView: public QTreeView
{
public:
MyView(QWidget *parent = 0);

protected:
void dropEvent(QDropEvent *event);
};

#endif


The only function I am interested in overriding is dropEvent so therefore it is the only protected member that I am overriding.



void MyView::dropEvent(QDropEvent *event)
{
QModelIndex curIndex = this->selectionModel()->currentIndex();

// There are two private variables I would like MyView to have access to from the parent (QWidget in this case)
// TreeModel & QHash; named tree and hashTable
// I would like for the following to happen:

// urnTree private variable in QWidget parent
//TreeItem *treeItem = tree->getItem(curIndex);

// hashTable would be same concept..
}


Is there a way to do what I am trying to do? I know this isn't directly related to Qt programming (more C++ question) but I have never had a need to do what I am thinking about trying to do here.

Thanks for any help!

Lykurg
6th December 2010, 23:18
It's a basic principle of oop that you can never reach a private member. So you can't. But to be honest I haven't understand which two private variables of QWidget do you try to reach. Normally all important variables have getter and setter functions. And I can't see an urnTree variable in QWidget.

jshafferman
7th December 2010, 01:29
Yeah I was thinking of making MyView a friend class to QWidget to gain access to its private variables. The urnTree is made from a class that is a sublcass of AbstractItemModel so it is a custom class and the class that has access to it called UrnTab is a subclass of QWidget. I didn't really think it would be possible to do what I am trying to do but I thought I would ask and find out :).

Here is the 'parent' class that has the private members I am talking about in it:



#ifndef URNTAB_H
#define URNTAB_H

#include <QWidget>
#include <QFile>
#include <QHash>
#include <QModelIndex>

#include "UrnInfo.h"

class TreeModel;
class MyView;
class UrnInfoDialog;
class QSplitter;

class UrnTab : public QWidget
{
Q_OBJECT

public:
UrnTab(QFile &inFile, QWidget *parent = 0);

//...

public slots:
//...
private slots:
//...

private:

TreeModel *urnTree;

MyView *view;

//...

QHash<int, GlobalUrnInfo> urnData;

//....
};


Obviously I want MyView to have access to urnTree and urnData since in the UrnTab constructor I do view = new MyView(this) where 'this' is the UrnTab object. I hope that clears up what I am trying to do.

Thanks again for any help!

Added after 1 54 minutes:

Ok so I need to redirect this question because I just realized that I don't necessarily need access to urnTree and urnData, what I need is access to a public function called updateParent(QModelIndex) inside of UrnTab. I haven't used a ton of polymorphism in awhile so I am kinda rusty on how to go about doing this. Basically I want MyView to be able to call this function inside of the dropEvent. I believe there is a way to do this but I am starting to run into OOP principles I haven't used in awhile and I am not sure if I am forgetting something or its plan not possible.

Here is the example of the call:



void MyView::dropEvent(QDropEvent *event)
{
QPoint position = event->pos();

QModelIndex curIndex = this->indexAt(position);

// call updateParent(curIndex) function in UrnTab which is the parent object to MyView
// MyView is built inside of the UrnTab constructor with UrnTab as its parent object i.e. MyView *view = new MyView(this) where 'this' is UrnTab
}

marcvanriet
7th December 2010, 12:28
What would you need to call this updateparent method for ?

I think that for your application you can use the normal setParent() method. It will take care of all the parent and children updating for you. No need to mess around.

Best regards,
Marc

jshafferman
8th December 2010, 17:30
I was able to solve the issue I was having, basically it was a poor initial design by me. I was able to move the TreeModel class and QHash into MyView class. The MyView class was a subclass from QTreeView which I then reimplemented the dropAction to suit my needs.

I think I was unclear on what I wanted to do but the updateParent(QModelIndex index) directly manipulated a class (TreeModel) that was a subclass of QAbstractItemModel therefore I needed MyView to have access to the TreeModel in order for the drag/drop functionality to be properly implemented.

Thanks for all your help everyone!