January 30, 2012

aurora aurora
Lab Rat
137 posts

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…

  1.     QToolBox *temp=new QToolBox;
  2.     temp=dynamic_cast<QToolBox *>(tempCheckBox->parentWidget()->parentWidget());
  3.     QString fname=temp->itemText(temp->currentIndex());
  4.     std::cout<<"FILE NAME:"<<fname.toStdString()<<endl;

also tried using
  1. temp=dynamic_cast<QToolBox *>(tempCheckBox->parentWidget());

but program was crashing both time….

5 replies

January 30, 2012

stima_ua stima_ua
Lab Rat
65 posts

it’s because you do wrong thing. I can not say that it is, but mb:

  1. tempCheckBox->parentWidget()->parentWidget()

return not QToolBox, and when you try get index app crash.

January 30, 2012

Andre Andre
Area 51 Engineer
6031 posts

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

  1. void dumpAncestors(QWidget* widget)
  2. {
  3.   if (!widget)
  4.     return;
  5.  
  6.   QWidget* parent = widget->parentWidget();
  7.   if (!parent) {
  8.     qDebug() << "Wiget has no parent.";
  9.     return;
  10.   }
  11.  
  12.   while (parent) {
  13.     qDebug() << "parent class:" << parent->metaObject()->className();
  14.     parent = parent->parentWidget();
  15.   }
  16. }

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

January 30, 2012

miroslav miroslav
Ant Farmer
228 posts

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.

 Signature 

Mirko Boehm | .(JavaScript must be enabled to view this email address) | KDE e.V.
FSFE Fellow
Qt Certified Specialist

January 31, 2012

aurora aurora
Lab Rat
137 posts

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?

  1. void FilterColl::getParentPage(QCheckBox *tempCheckBox)
  2.     {
  3.        QToolBox *temp=new QToolBox;
  4.        if(!tempCheckBox)
  5.        {
  6.            cout<<"no check box"<<endl;
  7.            return;
  8.        }
  9.        QWidget *parent=tempCheckBox->parentWidget();
  10.        if(!parent)
  11.        {
  12.            cout<<"np page for this widget"<<endl;
  13.            return;
  14.        }
  15.  
  16.     while(parent)
  17.     { QString classname=parent->metaObject()->className();
  18.         cout<<"the parent class is:"<<classname.toStdString() <<endl;
  19.         if(classname=="QToolBox")
  20.         {   temp=dynamic_cast<QToolBox *>(parent);
  21.             std::cout<<"the tool box page name:"<<temp->itemText(temp->indexOf(tempCheckBox)).toStdString()<<endl;
  22.             cout<<"the index is :"<<temp->indexOf(tempCheckBox)<<endl;
  23.         }
  24.         parent=parent->parentWidget();
  25.     }
  26. }

February 1, 2012

Volker Volker
Robot Herder
5428 posts

  1. QToolBox *toolBox = new QToolBox(this);
  2.  
  3. QWidget *page1 = new QWidget(this);
  4. QWidget *page2 = new QWidget(this);
  5. QWidget *page3 = new QWidget(this);
  6.  
  7. // fill the pages with the contents here
  8.  
  9. toolBox->addItem(page1, "Page 1");
  10. toolBox->addItem(page2, "Page 2");
  11. toolBox->addItem(page3, "Page 3");
  12.  
  13. // do something else
  14.  
  15. QCheckBox *checkBox = /* get it from somewhere */;
  16. QWidget *page = checkBox->parentWidget();
  17.  
  18. int index = toolBox->indexOf(page);
  19. QString pageTitle = toolBox->itemText(index);

Brain to terminal, not tested.

 
  ‹‹ find and replace certain strings in Rich Text Documents (odt, doc, docx, etc)      Question about January 2012 release for QTSDK 1.2 with Qt 4.8 ››

You must log in to post a reply. Not a member yet? Register here!