PDA

View Full Version : Is there any potential risk when getting children of QObject?



nifei
10th February 2009, 03:01
For example, is there any potential risk in the following code?


// when re-implement QCalendarWidget's functions
QObjectList children_of_this = this->children();
//this points to QCalendarWidget
for( int i = 0; i < children_of_this.count(); i++)
{
if(children_of_this.at(i)->objectName() == "qt_calendar_navigationbar")
{
navigator = qobject_cast<QWidget*>(children_of_this[i]);
if(navigator)
{
// do something to navigator's layout
}
//navigator is a pointer to a QWidget, which is previous NULL
if(children_of_this.at(i)->objectName() == "qt_calendar_calendarview")
{
view = qobject_cast<QTableView*>(children_of_this[i]);
if(view)
{
view->setItemDelegate(new QStyledItemDelegate);
}
}
//view is a pointer to a QTableView which is previous NULL
}


and in the "qcalendarwidget.cpp", the Qt's source code:


//i want to access objects like this
d->m_view->setObjectName(QLatin1String("qt_calendar_calendarview"));

navBarBackground = new QWidget(widget);
navBarBackground->setObjectName(QLatin1String("qt_calendar_navigationbar"));



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.

talk2amulya
10th February 2009, 10:56
well, there seems nothing wrong with it...make sure "this" and "widget" are pointing to the same object

nifei
11th February 2009, 02:53
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?

talk2amulya
11th February 2009, 05:41
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

jpn
11th February 2009, 18:21
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.