scrollArea->setWidget() reusing widget crashes program



  • Hey,
    I have this private slot function in MainWindow:

    void MainWindow::on_tabWidget_currentChanged(int index)
    {
        if(allNodesWidgets[index]){ //allNodesWidgets is a QVector of widgets
            
            ui->allNodes_scrollArea->setWidget(allNodesWidgets[index]);
    
        }
    }
    

    When I have two tabs, I can go from tab one to tab two but not back to tab one otherwise it crashes.
    In the debugger everything seems ok, but anyway it crashes...
    the vector has all elements it should have, the index is what I expect it to be, but anyway it crashes.
    What am I missing? (Graph is my scrollArea)
    0_1501576161318_Qt 6.png
    This pic was taken after I tried to go from tab two to tab one
    Any ideas?
    Thanks for answers



  • the scroll area will take ownership of the widget and delete the old one on a call to setWidget

    From http://doc.qt.io/qt-5/qscrollarea.html#setWidget

    The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.

    You must call takeWidget() before calling setWidget



  • @VRonin
    Ok, thanks, it worked.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.