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. Get widget from QTabWidget
Forum Updated to NodeBB v4.3 + New Features

Get widget from QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 392 Views
  • 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,

    I have created a "SectionsManager" class that publicly inherits from QTabWidget. I created a object of this class and a new tab:

    Section *section = new Section();
    addTab(section, section->getSectionName());

    "section" is an object of the Section class which inherits publicly from the Widget class. In the further part of the code I would like to get from the given tab about the index and extract the widget that I placed there earlier

    Section *section = ui->sectionManager->widget(index);

    however, it gets an error during compilation:
    "invalid conversion from 'QWidget' to 'Section'

    What could I do to fix it and be able to get from the given index of the tab the ptr of the object which I previously placed there

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2
      Section *section = qobject_cast<Section*>(ui->sectionManager->widget(index));
      
      C 1 Reply Last reply
      1
      • B Bonnie
        Section *section = qobject_cast<Section*>(ui->sectionManager->widget(index));
        
        C Offline
        C Offline
        Creatorczyk
        wrote on last edited by
        #3

        @Bonnie I did as you said and actually the code is compiling. However, I have a problem when I try use the methods of this object's class. The application is crashed.

        QString name = section->getSectionName();

        Do you know what could be the reason?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          Probably because section equals nullptr.
          You should add some protection when you use the cast so that it won't crash.

          if(Section *section = qobject_cast<Section*>(ui->sectionManager->widget(index))) {
            QString name = section->getSectionName();
          }
          

          Are you sure the result of ui->sectionManager->widget(index) is a Section?
          If section equals nullptr, that means it isn't.
          BTW, you can get the tab name by

          QString QTabWidget::tabText(int index) const
          
          C 1 Reply Last reply
          2
          • B Bonnie

            Probably because section equals nullptr.
            You should add some protection when you use the cast so that it won't crash.

            if(Section *section = qobject_cast<Section*>(ui->sectionManager->widget(index))) {
              QString name = section->getSectionName();
            }
            

            Are you sure the result of ui->sectionManager->widget(index) is a Section?
            If section equals nullptr, that means it isn't.
            BTW, you can get the tab name by

            QString QTabWidget::tabText(int index) const
            
            C Offline
            C Offline
            Creatorczyk
            wrote on last edited by
            #5

            @Bonnie It's works, thanks

            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