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. Remove tabs from QTabWidget when current index changed
Forum Updated to NodeBB v4.3 + New Features

Remove tabs from QTabWidget when current index changed

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • M Offline
    M Offline
    Maluna34
    wrote on last edited by
    #1

    Hello !

    I have an application managing a file editor with QTabWidget, and when the current index changed, I check that the opening file still exists on the disk, and if not, I ask the user if we wants to keep the file in the editor.
    If he chooses to remove it, then it changes the tab again, so all tabs can be closed like this.

    The problem is that I have a crash if all files are closed each time the current index changes.
    Here is the standalone code to reproduce it:

    #include <QApplication>
    #include <QTabWidget>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTabWidget tab;
    
        tab.addTab(new QWidget, "1");
        tab.addTab(new QWidget, "2");
        tab.addTab(new QWidget, "3");
    
        QObject::connect(&tab, &QTabWidget::currentChanged, [&tab](int index) {
            if (index == -1) {
                return;
            }
    
            // [2]
            // We imagine that it cheks that the file is not on the disk anymore,
            // and we asked the user for removing the associated tab.
            QWidget *tabWidget = tab.widget(index);
            if (tabWidget) {
                tab.removeTab(index);     // currentChanged is emitted and the slot is called again here!
                tabWidget->deleteLater();
            }
        });
    
        tab.show();
    
        // [1] Try to remove all tabs
        while (tab.count() > 0) {
            QWidget *tabWidget = tab.widget(0);
            if (tabWidget) {
                tab.removeTab(0);
                tabWidget->deleteLater();
            }
        }
    
        return a.exec();
    }
    

    At [1], I simulate a "close all files" action, but at the first iteration, they all are prematurely removed by currentChanged slot at [2].
    The crash happens inside the removeTab function in the lambda. Each time we switch to a new tab, the current function pauses at removeTab and we have 3 executions of removeTab in the stack. When we come back, it's like Qt is not able to finish the execution of removeTab.

    Thanks for your help!!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      Your lambda function is called recursively, that's not good.
      I would suggest to connect with Qt::QueuedConnection type to avoid that.

      QObject::connect(&tab, &QTabWidget::currentChanged, &tab, [&tab](int index) {...}, Qt::QueuedConnection);
      
      1 Reply Last reply
      4
      • M Offline
        M Offline
        Maluna34
        wrote on last edited by
        #3

        Oh great thank you!!

        Just to be sure, is the problem coming from the lambda specifically? I tried with a dedicated slot, it also seems to solve the problem.

        The rule is not to emit a signal from the triggered slot, is that it?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Maluna34 said in Remove tabs from QTabWidget when current index changed:

          The rule is not to emit a signal from the triggered slot, is that it?

          correct

          I tried with a dedicated slot, it also seems to solve the problem.

          I doubt so - there is no difference of a member function or a lambda here

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • M Offline
            M Offline
            Maluna34
            wrote on last edited by
            #5

            Thank you all! :)

            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