Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it possible to get QToolBox widget's page name, in run time?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    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....

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stima_ua
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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();
        }
        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          miroslav
          wrote on last edited by
          #4

          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.

          Mirko Boehm | mirko@kde.org | KDE e.V.
          FSFE Fellow
          Qt Certified Specialist

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aurora
            wrote on last edited by
            #5

            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();
            }
            

            }
            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              @
              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved