Is it possible to get QToolBox widget's page name, in run time?
-
I have a QToolBox and adding pages to it in runtime
Each page has scroll area, inside scroll area comboboxes area dded at runtime
Is it possible to get page name of any particular combobox?i tried code shown below, but it didnt work...
@
QToolBox *temp=new QToolBox;
temp=dynamic_cast<QToolBox *>(tempCheckBox->parentWidget()->parentWidget());
QString fname=temp->itemText(temp->currentIndex());
std::cout<<"FILE NAME:"<<fname.toStdString()<<endl;
@
also tried using
@
temp=dynamic_cast<QToolBox *>(tempCheckBox->parentWidget());
@
but program was crashing both time.... -
it's because you do wrong thing. I can not say that it is, but mb:
@tempCheckBox->parentWidget()->parentWidget()@
return not QToolBox, and when you try get index app crash. -
A bit of code like this will print out the parent classes so you can see the real object hierarchy:
note: brain to terminal, not tested
@
void dumpAncestors(QWidget* widget)
{
if (!widget)
return;QWidget* parent = widget->parentWidget();
if (!parent) {
qDebug() << "Wiget has no parent.";
return;
}while (parent) {
qDebug() << "parent class:" << parent->metaObject()->className();
parent = parent->parentWidget();
}
}
@ -
As a generic comment, whenever using dynamic_cast (or object_cast, any of those that return 0 if the cast failed), it is always recommendable to check the result for 0 before continuing. If it is "certain" that the cast should not fail, use an assert. Doing so will always catch problems right when they occur, not later when some other element is accessed through a zero pointer.
-
Thank u all...
But how can i get page name in QToolBox....
I tried as below....i got pointer to QToolBox....
But whenever i try to get page name of any widget it just returns nothing...always index is -1....:(
please tell me whats wrong here?@
void FilterColl::getParentPage(QCheckBox *tempCheckBox)
{
QToolBox *temp=new QToolBox;
if(!tempCheckBox)
{
cout<<"no check box"<<endl;
return;
}
QWidget *parent=tempCheckBox->parentWidget();
if(!parent)
{
cout<<"np page for this widget"<<endl;
return;
}while(parent) { QString classname=parent->metaObject()->className(); cout<<"the parent class is:"<<classname.toStdString() <<endl; if(classname=="QToolBox") { temp=dynamic_cast<QToolBox *>(parent); std::cout<<"the tool box page name:"<<temp->itemText(temp->indexOf(tempCheckBox)).toStdString()<<endl; cout<<"the index is :"<<temp->indexOf(tempCheckBox)<<endl; } parent=parent->parentWidget(); }
}
@ -
@
QToolBox *toolBox = new QToolBox(this);QWidget *page1 = new QWidget(this);
QWidget *page2 = new QWidget(this);
QWidget *page3 = new QWidget(this);// fill the pages with the contents here
toolBox->addItem(page1, "Page 1");
toolBox->addItem(page2, "Page 2");
toolBox->addItem(page3, "Page 3");// do something else
QCheckBox checkBox = / get it from somewhere */;
QWidget *page = checkBox->parentWidget();int index = toolBox->indexOf(page);
QString pageTitle = toolBox->itemText(index);
@Brain to terminal, not tested.