Sep-01-2019, 01:10 AM
When a dragged item hovers on a tab, I want the view to switch on this tab. The problem is two fold;
1. How to trigger new events once the mouse entered the tagBar
2. How to know which tab is being hovered
I tried subclassing the QTabBar, and catching dragMoveEvent, but the event is never triggered. Then I tried to reset or ignore the dragEnterEvent, so it might be trigered again without leaving the tabBar. What I tried are the commented lines below;
1. How to trigger new events once the mouse entered the tagBar
2. How to know which tab is being hovered
I tried subclassing the QTabBar, and catching dragMoveEvent, but the event is never triggered. Then I tried to reset or ignore the dragEnterEvent, so it might be trigered again without leaving the tabBar. What I tried are the commented lines below;
#!/usr/bin/python3 import sys from PyQt5 import QtCore, QtGui, QtWidgets class TabBar(QtWidgets.QTabBar): def __init__(self, parent): super().__init__() self.setAcceptDrops(True) def dragEnterEvent(self, event): print("enter", event) #event.ignore() #QtCore.QCoreApplication.postEvent(self, QtGui.QDragLeaveEvent()) #QtCore.QCoreApplication.sendEvent(self, QtGui.QDragLeaveEvent()) #QtCore.QObject.event(self, QtGui.QDragLeaveEvent()) #self.dragLeaveEvent(QtGui.QDragLeaveEvent()) # This event is never called def dragMoveEvent(self, event): print("move", event) class TabWidget(QtWidgets.QTabWidget): def __init__(self, parent): super().__init__() tab1 = QtWidgets.QTreeWidget() self.setTabBar(TabBar(self)) self.addTab(tab1, "1st tab") self.addTab(QtWidgets.QTreeWidget(), "2nd tab") self.addTab(QtWidgets.QTreeWidget(), "3rd tab") item = QtWidgets.QTreeWidgetItem() item.setText(0, "drag me") tab1.addTopLevelItem(item) tab1.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly) class Main(QtWidgets.QMainWindow): def __init__(self, parent): super().__init__() self.ui = QtWidgets.QWidget(self) self.ui.tab = TabWidget(self) self.ui.layout = QtWidgets.QVBoxLayout() self.ui.layout.addWidget(self.ui.tab) self.ui.setLayout(self.ui.layout) self.setCentralWidget(self.ui) self.show() if __name__== '__main__': app = QtWidgets.QApplication(sys.argv) gui = Main(app) sys.exit(app.exec_())I wonder if this is possible to do, as the tabBar is seen as a single object containing all the tabs, and I don't know how to access them directly. Any ideas?