QTabWidget: close tab button not working

For future stumblers upon this question looking for a PyQt5 solution, this can be condensed into a 1-liner:

tabs = QTabWidget()
tabs.tabCloseRequested.connect(lambda index: tabs.removeTab(index))

The tabCloseRequested signal emits an integer equal to the index of the tab that emitted it, so you can just connect it to a lambda function that takes the index as its argument.

The only problem I could see with this is that connecting a lambda function to the slot prevents the object from getting garbage collected when the tab is deleted (see here).

EDIT (9/7/21): The lambda function isn't actually necessary since QTabWidget.removeTab takes an integer index as its sole argument by default, so the following will suffice (and avoids the garbage-collection issue):

tabs.tabCloseRequested.connect(tabs.removeTab)

Create a slot e.g. closeMyTab(int) and connect the tab widget's tabCloseRequested(int) signal to this slot. In this slot call tab widget's removeTab method with the index received from the signal.

See this answer for more details.