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. How handle close tab event?
Forum Updated to NodeBB v4.3 + New Features

How handle close tab event?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.5k Views 2 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.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    I have:

    connect(tabWidget, &QTabWidget::tabCloseRequested, this, 
    [this](int index) {
            onCloseTab(index);
        });
    void TabWindow::onCloseTab(int index)
    {
        setWindowTitle("abc");
    }
    

    But is not called
    I also not found close events other widgets like QPlainTextEdit

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      did you set
      http://doc.qt.io/qt-5/qtabwidget.html#tabsClosable-prop
      Also, this is a signal, not event.
      CloseEvent are virtual function and you must subclass to override.

      Just tried fast sample with tabCloseRequested and it was called.
      So we have to investigate why in your sample its not called.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AndrzejB
        wrote on last edited by
        #3

        I don't know why close buttons on tabs not works

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Just to be sure, you did as @mrjj suggested and call setTabClosable ?

          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
          • A Offline
            A Offline
            AndrzejB
            wrote on last edited by
            #5
            #include "mainwindow.h"
            #include <QTabWidget>
            #include <QVBoxLayout>
            #include <QWidget>
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent)
            {
                QTabWidget *tabWidget = new QTabWidget();
                tabWidget->setTabsClosable(true);
                tabWidget->setMovable(true);
                tabWidget->addTab(new QWidget(),"1");
                tabWidget->addTab(new QWidget(),"2");
                QVBoxLayout *mainLayout = new QVBoxLayout;
                mainLayout->addWidget(tabWidget);
                QWidget *centralWidget = new QWidget;
                centralWidget->setLayout(mainLayout);
                setCentralWidget(centralWidget);
            }
            
            MainWindow::~MainWindow()
            {}
            
            1 Reply Last reply
            0
            • A Offline
              A Offline
              AndrzejB
              wrote on last edited by
              #6

              Solution:

              connect( tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(slotCloseTab()) );
              
              
              void MainWindow::slotCloseTab()
              {
                QWidget *tab = dynamic_cast<QWidget*>( tabWidget->currentWidget() );
                disconnect( tab, 0, 0, 0 );
                tab->close();
                delete tab;
                tab = NULL;
              }
              

              by [https://www.qtcentre.org/threads/46333-tabsClosable-not-working-!-how-to-close-tabs] but added delete tab

              mrjjM 1 Reply Last reply
              1
              • A AndrzejB

                Solution:

                connect( tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(slotCloseTab()) );
                
                
                void MainWindow::slotCloseTab()
                {
                  QWidget *tab = dynamic_cast<QWidget*>( tabWidget->currentWidget() );
                  disconnect( tab, 0, 0, 0 );
                  tab->close();
                  delete tab;
                  tab = NULL;
                }
                

                by [https://www.qtcentre.org/threads/46333-tabsClosable-not-working-!-how-to-close-tabs] but added delete tab

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @AndrzejB
                Hi
                oK. tried your code with

                 QTabWidget *tabWidget = new QTabWidget();
                    tabWidget->setTabsClosable(true);
                    tabWidget->setMovable(true);
                    tabWidget->addTab(new QWidget(), "1");
                    tabWidget->addTab(new QWidget(), "2");
                    QVBoxLayout *mainLayout = new QVBoxLayout;
                    mainLayout->addWidget(tabWidget);
                    QWidget *centralWidget = new QWidget;
                    centralWidget->setLayout(mainLayout);
                    setCentralWidget(centralWidget);
                    connect(tabWidget, &QTabWidget::tabCloseRequested, this,
                    [this](int index) {
                        qDebug() << "called";
                    });
                

                and that was called so i wondered if it was other tabWidget you connected or something like that.

                anyway, if using slotCloseTab works, i guess it fine.
                Just odd your first code/try didnt work.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You should rather use QTabWidget::widget and use the int parameter of the signal to get the widget that was requested to be closed. It might not be the one that's currently shown.

                  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
                  1

                  • Login

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