Results 1 to 9 of 9

Thread: Acces parent object

  1. #1
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Acces parent object

    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!

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Acces parent object

    "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:
    Qt Code:
    1. myMainWindow *mmw = qobject_cast<myMainWindow *>(parent);
    2. QAbstractItemModel *model = 0;
    3. // now check if mmw is not NULL, because if parent would be pointer to widget different than
    4. // your myMainWindow, qobject_cast returns NULL
    5. if (mmw) {
    6. model = mmw->model;
    7. }
    To copy to clipboard, switch view to plain text mode 

    P.S. And use CODE tags in your posts.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acces parent object

    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?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Acces parent object

    can you show us your whole project/files or prepare minimal compilable example reproducing the problem?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    May 2009
    Location
    Copenhagen
    Posts
    50
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acces parent object

    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!
    Attached Files Attached Files

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acces parent object

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acces parent object

    btw, you can't do this
    Qt Code:
    1. jourmodel = new QAbstractItemModel(this);
    To copy to clipboard, switch view to plain text mode 
    since QAbstractItemModel is abstract class.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Acces parent object

    add #include "mainwindow.h" in journalpanel.cpp and this line
    Qt Code:
    1. mw = qobject_cast<MainWindow *>(this->parent());
    To copy to clipboard, switch view to plain text mode 
    will be compiled.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Acces parent object

    or you can also try QWidget::parentWidget()
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. The following user says thank you to faldzip for this useful post:

    Nightfox (19th August 2009)

Similar Threads

  1. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 20:39
  2. Replies: 11
    Last Post: 6th March 2009, 22:15
  3. Replies: 4
    Last Post: 19th February 2009, 11:10
  4. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  5. Call on Parent Object
    By hufgardm in forum Newbie
    Replies: 1
    Last Post: 7th September 2006, 15:11

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.