I need to execute a block of code when the user clicks on the tab of a tabbified QDockWidget. So far I've been doing this via a hack using the "visibilityChanged" event but this is now causing issues (for example, if I have several tabbified dock widgets and I drag one out so that it is floating, the tabbified one underneath will fire its "visibilityChanged" event which I will mistakenly interpret as the user clicking the tab). How can I receive proper notification when a user clicks on a QDockWidgets' tab? I've experimented with the "focusInEvent" of QDockWidget but it doesn't seem to fire when the tab is clicked.
Asked
Active
Viewed 1,020 times
1
-
When a partitioned item is dragged, the click will also be triggered, is it okay for that to happen?, please provide a [mcve] – eyllanesc Jul 06 '18 at 20:52
-
I'm afraid the click event is of no help here. I just need to be notified when a user clicks on the tab. – LKeene Jul 06 '18 at 21:12
-
his phrase is contradictory, on the one hand it indicates that the clicked event will not be useful and on the other hand you want to be notified of the clicked. You want potatoes but you do not want the potato. – eyllanesc Jul 06 '18 at 21:24
-
If I handle the "mousePressEvent" in my QDockWidget, the event is not raised when the user selects the tab. Ergo it does not provide notification of when the tab is clicked and is not useful for this case. – LKeene Jul 06 '18 at 21:29
-
Why do you assume that I use that method? I use another method and it is launched when the tab is pressed. :) – eyllanesc Jul 06 '18 at 21:32
-
Can you please tell me what the method is that is launched when the tab is pressed? Note: I'm using PyQt 4.7 – LKeene Jul 06 '18 at 21:35
1 Answers
1
When you use tabifyDockWidget()
method QMainWindow
creates a QTabBar
, this is not directly accessible but using findChild()
you can get it, and then use the tabBarClicked
signal
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
first_dock = None
for i in range(10):
dock = QtGui.QDockWidget("title {}".format(i), self)
dock.setWidget(QtGui.QTextEdit()) # testing
self.addDockWidget(QtCore.Qt.TopDockWidgetArea, dock)
if first_dock:
self.tabifyDockWidget(first_dock, dock)
else:
first_dock = dock
dock.raise_()
tabbar = self.findChild(QtGui.QTabBar, "")
tabbar.tabBarClicked.connect(self.onTabBarClicked)
def onTabBarClicked(self, index):
tabbar = self.sender()
text = tabbar.tabText(index)
print("index={}, text={}".format(index, text))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

eyllanesc
- 235,170
- 19
- 170
- 241