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. [SOLVED] QTabWidget remove tab problem!
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTabWidget remove tab problem!

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 26.8k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    issam
    wrote on 10 May 2013, 22:46 last edited by
    #1

    Hi,

    In my application I use a QTabWidget instance as the QMainWindow central area. The tabwidget contains n tabs.
    This slot is to remove a tab :

    @
    void MainWindow::closeTab(int index)
    {
    QWidget * tab = m_parentTabWidget->widget(index);
    m_parentTabWidget->removeTab(index);
    //delete tab;
    }
    @

    When I close the last tab (m_parentTabWidget->count() == 1), the application crashes and I get the Error : Segmentation fault.

    help !

    http://www.iissam.com/

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on 11 May 2013, 01:12 last edited by
      #2

      Is there a child widget for the TabWidget? Maybe the SegFault is from the orphan widget?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        issam
        wrote on 11 May 2013, 14:33 last edited by
        #3

        No, there is no child widget for TabWidget !

        The error occurs only when I remove the last tab. For example, suppose that
        I have three tab in the TabWidget. When i remove tab 1, there is no error.
        When i remove tab 2, there is no error. But the application crashes when i remove
        tab 3. Also, when i call the method clear(), to remove all tabs I have the some error !

        http://www.iissam.com/

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 May 2013, 20:14 last edited by
          #4

          Hi,

          I would say: you don't have a widget at that index, it will be 1. It works twice because you have 3 widgets.

          A rule of thumb: never delete something that you havent check for its existence.

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            issam
            wrote on 11 May 2013, 20:42 last edited by
            #5

            Hi,
            Even this code led to the some error :
            @
            connect(this, SIGNAL(tabCloseRequested(int)),
            this, SLOT(closeTab(int)));

            void TabWidget::closeTab(int index)
            {
            removeTab(index);
            }
            @

            any suggestion ?

            http://www.iissam.com/

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hostel
              wrote on 12 May 2013, 17:10 last edited by
              #6

              Use valgrind and check for memory leaks and null pointers.

              You can also check your code by Cppcheck.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on 12 May 2013, 17:29 last edited by
                #7

                Is it possible at all to have a tab widget with zero tabs?

                Maybe you just remove the tab widget altogether, or at least hide it, instead?

                Also, what if you change your code to:
                @void MainWindow::closeTab(int index)
                {
                if((index < 0) || (index >= m_parentTabWidget->count()))
                {
                qWarning("Oups!!!!");
                return;
                }
                m_parentTabWidget->removeTab(index);
                }@

                If that doesn't reveal anything, make a DEBUG build and see where exactly it crashes...

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  issam
                  wrote on 12 May 2013, 19:08 last edited by
                  #8

                  Hi friends,

                  bq. MuldeR wrote : Is it possible at all to have a tab widget with zero tabs?

                  What made me crazy, is the fact that initially the TabWidget doesn't contain any tab and the application starts perfectly. The class document does not mention this trick. So, I have thought to my web Browser! When I close the last opened tab the browser close itself automatically! So I have already changed the TabWidget like this :

                  @
                  void MainWindow::closeTab(int index)
                  {
                  if(m_parentTabWidget->count() == 1)
                  return;

                      m_parentTabWidget->removeTab(index);
                  }
                  

                  @

                  thnk you :)

                  bq. Hostel wrote : Use valgrind and check for memory leaks and null pointers.

                  It would be a good tool !
                  Have you a good and simple quick-start other than the official one ?

                  thanks. :)

                  http://www.iissam.com/

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on 12 May 2013, 19:46 last edited by
                    #9

                    I don't think you need necessarily need something like Valgrind here.

                    Just make a DEBUG build of your app, link it with the DEBUG Qt libraries and then run your app in a debugger.

                    As soon as it crashes, check the callstack...

                    --

                    Anyway, if you are on Linux and want to try Valgrind, then see here:
                    http://valgrind.org/docs/manual/quick-start.html

                    Though this is more for finding memory leaks, uninitialized memory reads and stuff...

                    As long as your app does crash (reproducible), thing are usually straight forward in a "normal" debugger.

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      issam
                      wrote on 21 May 2013, 14:44 last edited by
                      #10

                      OK thanks. :)

                      http://www.iissam.com/

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Banshee
                        wrote on 1 Jul 2013, 14:04 last edited by
                        #11

                        Hello!
                        I have the same problem and found this thread.
                        Is there a better solution than just not deleting the last tab?
                        I have a class which is derived from QTabWidget. If I try to close the last tab (with "removeTab" or "clear"), the app crashes because of a segfault.
                        I dont think i handle some pointers wrong, because i can close every tab, as long as if there are more than just one.
                        My TabWidget starts with zero tabs, so why it is not possible to delete the last one?
                        Any suggestions?
                        Thank you.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          robocoptertron
                          wrote on 28 Mar 2021, 03:43 last edited by robocoptertron
                          #12

                          Hi there!

                          I know this thread is quite old, but I encountered a similar problem.

                          The issue for me was that I had registered a slot to handle the QTabWidget's 'currentChanged' signal.
                          When the last tab is closed in a QTabWidget, the 'currentChanged' signal is emitted with -1 as the argument
                          for the 'index' parameter, but the slot I had created to handle this signal was trying to access a tab (and no tabs exist
                          at this point).

                          So, I simply added the line:

                          if (index < 0) return; 
                          

                          to the start of the slot, and voila! Problem solved!

                          I hope that helps someone :)

                          1 Reply Last reply
                          2

                          • Login

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