Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabWidget: Add new Tab from a QWidget tab



  • Hi,

    I my application, I have a QTabWidget with several tabs.

    On tab of that QTabWidget is inside an QWidegt and I add this like

    ui->tabWidget->insertTab(1, new FormMember(0, this), "New Member");
    

    Until here, everything works fine.

    Now I am inside that FormMember Class (QWidget) and I like to create a new tab for my QTabWidget from there.

    I have no Idea how I can access to that QTabWidget (which is not part of my FormMember class) object to add a new tab.

    What would be the right way. Do I need to give a pointer as further parameter to my FormMember class or is there any better way to receive this.

    When I was working with c++ Builder 20 years ago it was no problem to call other object from other windows.



  • @Philipp-DE
    You should really do this from the ui->tabWidget level, or send it a signal asking it to create another tab.

    But if you do want to be able to access the tabWidget from your FormMember, as you say you could pass the tabWidget for FormMember to store (naughty), or you can use FormMember's parent() or parentWidget() (I think the latter) to walk up the hierarchy (you may have to go a couple of steps, not just one, can't recall) till you hit a qobject_cast<QTabWidget*>(parent) != nullptr, and that will be the parent QTabWidget.


  • Lifetime Qt Champion

    Hi,

    Use @JonB first suggestion. Implement a proper API for that. Toying with parent widgets is usually sign of a wrong design.


Log in to reply