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
Forum Update on Monday, May 27th 2025

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.
  • N Offline
    N Offline
    Niagarer
    wrote on 1 Aug 2017, 08:31 last edited by Niagarer 8 Jan 2017, 08:42
    #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
    • V Offline
      V Offline
      VRonin
      wrote on 1 Aug 2017, 10:03 last edited by mrjj 8 Jan 2017, 12:07
      #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

      N 1 Reply Last reply 1 Aug 2017, 16:00
      3
      • V VRonin
        1 Aug 2017, 10:03

        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

        N Offline
        N Offline
        Niagarer
        wrote on 1 Aug 2017, 16:00 last edited by
        #3

        @VRonin
        Ok, thanks, it worked.

        1 Reply Last reply
        0

        3/3

        1 Aug 2017, 16:00

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved