You are still producing a memory leak, and your code works if you fill the QToolBox correctly!
#include <QtGui>
{
if (!tempCheckBox) {
qWarning() << "no check box";
return;
}
QWidget *parent
= tempCheckBox
->parentWidget
();
if (!parent) {
qWarning() << "np page for this widget";
return;
}
while (parent) {
QString classname
= parent
->metaObject
()->className
();
qWarning() << "the parent class is:" << classname;
if (classname == "QToolBox") {
temp = dynamic_cast<QToolBox *>(parent);
qWarning() << "the tool box page name:" << temp->itemText(temp->indexOf(tempCheckBox));
qWarning() << "the index is :" << temp->indexOf(tempCheckBox);
}
parent = parent->parentWidget();
}
}
int main(int argc, char **argv)
{
tb.insertItem(0, &b, "sample text");
getParentPage(&b);
return 0;
}
#include <QtGui>
void getParentPage(QCheckBox *tempCheckBox)
{
QToolBox *temp = 0;
if (!tempCheckBox) {
qWarning() << "no check box";
return;
}
QWidget *parent = tempCheckBox->parentWidget();
if (!parent) {
qWarning() << "np page for this widget";
return;
}
while (parent) {
QString classname = parent->metaObject()->className();
qWarning() << "the parent class is:" << classname;
if (classname == "QToolBox") {
temp = dynamic_cast<QToolBox *>(parent);
qWarning() << "the tool box page name:" << temp->itemText(temp->indexOf(tempCheckBox));
qWarning() << "the index is :" << temp->indexOf(tempCheckBox);
}
parent = parent->parentWidget();
}
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWidget w;
QToolBox tb(&w);
QCheckBox b;
tb.insertItem(0, &b, "sample text");
getParentPage(&b);
return 0;
}
To copy to clipboard, switch view to plain text mode
gives
the parent class is: "QWidget"
the parent class is: "QScrollArea"
the parent class is: "QToolBox"
the tool box page name: "sample text"
the index is : 0
the parent class is: "QWidget"
Bookmarks