0

So to close tabs I've been using the QTabWidget.currentWidget() to find the selected tab to close, but now when I click the close icon on a different tab, it closes the current tab because of how I have set this up

So how can I find the tab attached the the close button so I can close the correct tab?

Cheers

SketchyManDan
  • 176
  • 1
  • 10

1 Answers1

4

Please handle with void tabCloseRequested (int) to get current index of widget has been request close. Next, find this widget with index by QWidget QTabWidget.widget (self, int index) and delete it. Or, use QTabWidget.removeTab (self, int index) (But The page widget itself is not deleted).

import sys
from PyQt4 import QtGui

class QCustomTabWidget (QtGui.QTabWidget):
    def __init__ (self, parent = None):
        super(QCustomTabWidget, self).__init__(parent)
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.closeTab)
        for i in range(1, 10):
            self.addTab(QtGui.QWidget(), 'Tab %d' % i)

    def closeTab (self, currentIndex):
        currentQWidget = self.widget(currentIndex)
        currentQWidget.deleteLater()
        self.removeTab(currentIndex)

myQApplication = QtGui.QApplication([])
myQCustomTabWidget = QCustomTabWidget()
myQCustomTabWidget.show()
sys.exit(myQApplication.exec_())
Bandhit Suksiri
  • 3,390
  • 1
  • 18
  • 20