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. scrollArea->setWidget() reusing widget crashes program
QtWS25 Last Chance

scrollArea->setWidget() reusing widget crashes program

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by mrjj
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      NiagarerN 1 Reply Last reply
      3
      • VRoninV VRonin

        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

        NiagarerN Offline
        NiagarerN Offline
        Niagarer
        wrote on last edited by
        #3

        @VRonin
        Ok, thanks, it worked.

        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