Results 1 to 5 of 5

Thread: Is there any potential risk when getting children of QObject?

  1. #1
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Is there any potential risk when getting children of QObject?

    For example, is there any potential risk in the following code?
    Qt Code:
    1. // when re-implement QCalendarWidget's functions
    2. QObjectList children_of_this = this->children();
    3. //this points to QCalendarWidget
    4. for( int i = 0; i < children_of_this.count(); i++)
    5. {
    6. if(children_of_this.at(i)->objectName() == "qt_calendar_navigationbar")
    7. {
    8. navigator = qobject_cast<QWidget*>(children_of_this[i]);
    9. if(navigator)
    10. {
    11. // do something to navigator's layout
    12. }
    13. //navigator is a pointer to a QWidget, which is previous NULL
    14. if(children_of_this.at(i)->objectName() == "qt_calendar_calendarview")
    15. {
    16. view = qobject_cast<QTableView*>(children_of_this[i]);
    17. if(view)
    18. {
    19. view->setItemDelegate(new QStyledItemDelegate);
    20. }
    21. }
    22. //view is a pointer to a QTableView which is previous NULL
    23. }
    To copy to clipboard, switch view to plain text mode 

    and in the "qcalendarwidget.cpp", the Qt's source code:
    Qt Code:
    1. //i want to access objects like this
    2. d->m_view->setObjectName(QLatin1String("qt_calendar_calendarview"));
    3.  
    4. navBarBackground = new QWidget(widget);
    5. navBarBackground->setObjectName(QLatin1String("qt_calendar_navigationbar"));
    To copy to clipboard, switch view to plain text mode 

    I'm quite new to both Qt and C++ and wondering whether or not it is right to get the objects pointers those are not exposed to users.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any potential risk when getting children of QObject?

    well, there seems nothing wrong with it...make sure "this" and "widget" are pointing to the same object

  3. The following user says thank you to talk2amulya for this useful post:

    nifei (11th February 2009)

  4. #3
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any potential risk when getting children of QObject?

    Quote Originally Posted by talk2amulya View Post
    well, there seems nothing wrong with it...make sure "this" and "widget" are pointing to the same object
    Of course they point to the same object. However, I don't think these objects are supposed to be accessed by users, are they?

  5. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any potential risk when getting children of QObject?

    i dont understand what u mean by whether it should be accessible to the user. All you are doing is providing a navigator and view using the parent. I dont think there is any "risk" in that, although i dont totally understand ur context

  6. The following user says thank you to talk2amulya for this useful post:

    nifei (12th February 2009)

  7. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is there any potential risk when getting children of QObject?

    Quote Originally Posted by nifei View Post
    However, I don't think these objects are supposed to be accessed by users, are they?
    You're right in the sense that nobody guarantees that for example "qt_calendar_navigationbar" exists in the future. You're on your own when you rely on internal parts of Qt.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    nifei (12th February 2009)

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.