PDA

View Full Version : Is it possible to get QToolBox widget's page name, in run time?



aurora
30th January 2012, 04:42
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....

Lykurg
30th January 2012, 09:59
Well in your first example you are creating a memory leak. Beside that, are you sure tempCheckBox isn't a null pointer?

aurora
30th January 2012, 10:18
Ya thats not null pointer...

aurora
31st January 2012, 06:04
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();
}
}

Lykurg
31st January 2012, 07:19
You are still producing a memory leak, and your code works if you fill the QToolBox correctly!
#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;
}

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"

aurora
31st January 2012, 11:28
You are still producing a memory leak,

Sorry i'm new to Qt, please let me how to avoid memory leak..?
Where i'm producing memory leak in this program?
any links to learn abt that?

Lykurg
31st January 2012, 13:01
Why you write
QToolBox *temp=new QToolBox;? There you create a tool box without deleting it. This creates a memory leak...