Your segmentation faults come from assuming pointers are valid and point to things of certain types:
void Class::UpdateFont()
{
QObject *obj
= this
->parent
();
// this can be NULL QObjectList objlistChildren = obj->children(); // which will crash here
int iCount = objlistChildren.count();
for(int iIndex = 0; iIndex < iCount; iIndex++)
{
QObject *temp
= objlistChildren
[iIndex
];
( (QWidget*)objlistChildren
[iIndex
] )->setFont
(font
);
// what do you suppose happens if the child is not a QWidget? }
}
void Class::UpdateFont()
{
QObject *obj = this->parent(); // this can be NULL
QObjectList objlistChildren = obj->children(); // which will crash here
int iCount = objlistChildren.count();
for(int iIndex = 0; iIndex < iCount; iIndex++)
{
QObject *temp = objlistChildren[iIndex];
( (QWidget*)objlistChildren[iIndex] )->setFont(font); // what do you suppose happens if the child is not a QWidget?
}
}
To copy to clipboard, switch view to plain text mode
You should get out of the habit of using C-style casts preferring C++-style casts of the correct type. Casts in general are last-resort mechanisms and worthy of avoidance. In this circumstance you could use qobject_cast and check the cast was successful.
Bookmarks