Important: Please read the Qt Code of Conduct -

QTabWidget/QScrollArea unable to display widgets dinamically

  • Hello guys,

    I am trying to add a new widget to a couple of tabs but no success.

    @QTabWidget *tt = new QTabWidget();

    for(i = serversInfo.begin(); i!= serversInfo.end(); i++){

        tt->addTab(new QScrollArea(new StandardWidget()), i.key());

    After applying this code all the tabs are empty .

    Any sugestion will be very appreciated.

    Kind Regards,

  • The scroll area does not have a QScrollArea (widget to control) constructor but only a QScroll(parent widget) constructor. So in basic you say to the QScrollArea that his parent is StandardWidget. That is valid, because that is just a widget so capable of owning a child. Then you set that empty QScrollArea onto the tabwidet. So all works according your code!
    It should be:
    QScrollArea Scroll = new ScrollArea(this) ; // or scroll = new SCrollArea();
    Scroll->setWidget(new StandardWidget); // be aware that you might loose control of the standardwidget if you do not store the pointers into class members.
    tt->addTab(Scroll, i.key()); // The tab widget does not own the widget, so no child/parent relationship. When removing tabs, the scroll widget will remain in your memory and might cause memorly leakage!

Log in to reply