Results 1 to 5 of 5

Thread: Can you access the parent widget of a subclass

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Can you access the parent widget of a subclass

    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:

    Qt Code:
    1. #ifndef MYVIEW_H
    2. #define MYVIEW_H
    3.  
    4. #include <QTreeView>
    5.  
    6. class MyView: public QTreeView
    7. {
    8. public:
    9. MyView(QWidget *parent = 0);
    10.  
    11. protected:
    12. void dropEvent(QDropEvent *event);
    13. };
    14.  
    15. #endif
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void MyView::dropEvent(QDropEvent *event)
    2. {
    3. QModelIndex curIndex = this->selectionModel()->currentIndex();
    4.  
    5. // There are two private variables I would like MyView to have access to from the parent (QWidget in this case)
    6. // TreeModel & QHash; named tree and hashTable
    7. // I would like for the following to happen:
    8.  
    9. // urnTree private variable in QWidget parent
    10. //TreeItem *treeItem = tree->getItem(curIndex);
    11.  
    12. // hashTable would be same concept..
    13. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Can you access the parent widget of a subclass

    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can you access the parent widget of a subclass

    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:

    Qt Code:
    1. #ifndef URNTAB_H
    2. #define URNTAB_H
    3.  
    4. #include <QWidget>
    5. #include <QFile>
    6. #include <QHash>
    7. #include <QModelIndex>
    8.  
    9. #include "UrnInfo.h"
    10.  
    11. class TreeModel;
    12. class MyView;
    13. class UrnInfoDialog;
    14. class QSplitter;
    15.  
    16. class UrnTab : public QWidget
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. UrnTab(QFile &inFile, QWidget *parent = 0);
    22.  
    23. //...
    24.  
    25. public slots:
    26. //...
    27. private slots:
    28. //...
    29.  
    30. private:
    31.  
    32. TreeModel *urnTree;
    33.  
    34. MyView *view;
    35.  
    36. //...
    37.  
    38. QHash<int, GlobalUrnInfo> urnData;
    39.  
    40. //....
    41. };
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void MyView::dropEvent(QDropEvent *event)
    2. {
    3. QPoint position = event->pos();
    4.  
    5. QModelIndex curIndex = this->indexAt(position);
    6.  
    7. // call updateParent(curIndex) function in UrnTab which is the parent object to MyView
    8. // 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
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jshafferman; 7th December 2010 at 01:29.

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can you access the parent widget of a subclass

    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

  5. #5
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: Can you access the parent widget of a subclass

    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!

Similar Threads

  1. QGraphicsItem subclass access to QGraphicsView size
    By rubenvb in forum Qt Programming
    Replies: 4
    Last Post: 23rd January 2010, 21:36
  2. Replies: 7
    Last Post: 14th January 2010, 08:47
  3. Replies: 4
    Last Post: 3rd October 2009, 08:19
  4. How can I access public variable parent Form
    By validator in forum Qt Programming
    Replies: 14
    Last Post: 18th December 2008, 21:12
  5. QPainter and how to access the parent variables.
    By Teuniz in forum Qt Programming
    Replies: 6
    Last Post: 7th March 2007, 14:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.