Results 1 to 3 of 3

Thread: Dynamic type of parent outside the constructor

  1. #1
    Join Date
    Mar 2011
    Posts
    2

    Default Dynamic type of parent outside the constructor

    Hi guys, I have a question. I'm making a widget (let's call it myWidget) which inherits from QWidget and my window widget (the top one in the parent hierarchy) is a MainWindow (made by me) which inherits from QMainWindow. The question is, why something like this doesn't work ?
    Qt Code:
    1. class myWidget : public QWidget {
    2. ...
    3. public:
    4. myWidget(QWidget* parent =0) {}
    5. ...
    6. };
    7.  
    8. // a slot I made
    9. void myslot() {
    10. ...
    11. MainWindow* p = static_cast<MainWindow*>(window());
    12. ...
    13. }
    To copy to clipboard, switch view to plain text mode 
    The cast doesn't work, even with a dynamic_cast its the same. Why ?

  2. #2
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamic type of parent outside the constructor

    It's not quite clear what's goin on here.I mean,how does MainWindow class relate to myWidget class?What does the slot belong to? Maybe i didn't understand the class hierachy right but by now,it seems like you're trying to cast essentially inappropriate types...

  3. #3
    Join Date
    Mar 2011
    Posts
    2

    Default Re: Dynamic type of parent outside the constructor

    Ah sorry I think I figured out what's going on. I thought that the function window() would return the top level widget in the parent hierarchy, whereas it returns the first window widget in the hierarchy. So it was a bad type cast. By the way, is there any built-in function that returns the top level widget ? I mean, something like

    Qt Code:
    1. QObject* top_level_widget(QObject* q) {
    2. while(q->parent())
    3. q = q->parent();
    4. return q;
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Dynamic plugin: accessing parent's methods
    By codeslicer in forum Qt Programming
    Replies: 1
    Last Post: 4th January 2011, 09:16
  2. QTableWidgetItem dynamic type, how?!
    By Onanymous in forum Newbie
    Replies: 2
    Last Post: 13th May 2010, 13:29
  3. Replies: 1
    Last Post: 22nd April 2010, 14:34
  4. Replies: 2
    Last Post: 22nd April 2010, 11:44
  5. Dynamic pointer type
    By estanisgeyer in forum General Programming
    Replies: 3
    Last Post: 9th October 2008, 16:51

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.