Search code examples
python-2.7pyqt4qtabwidget

How to make QTabWidget clickable?


i searched about this topic and find some examples how to make qwidget clickable but just to print texts like this one import sys

from PyQt4.QtGui import QWidget, QApplication

class MyWidget(QWidget):
    def mousePressEvent(self, event):
        print "clicked"

app = QApplication(sys.argv)

widget = MyWidget()
widget.show()

app.exec_()

what i need is it possible to make th QTabWidget clickable and when click on it we can use it like a button to open a file for example ?


Solution

  • What you should do is create a signal and output it as shown below:

    class ClickableQTabWidget(QTabWidget):
        clicked = pyqtSignal()
        def mousePressEvent(self, event):
            self.clicked.emit()
    
    class Widget(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.setLayout(QVBoxLayout())
            self.cw = ClickableQTabWidget(self)
            self.layout().addWidget(self.cw)
            self.cw.clicked.connect(self.onClicked)
    
        def onClicked(self):
            print("clicked")
    
    app = QApplication(sys.argv)
    
    widget = Widget()
    widget.show()
    
    app.exec_()