Re: QTabWidget tab movement?
Matic Kukovec <[email protected]>
| Newsgroups | gmane.comp.python.pyqt-pykde |
|---|---|
| Message-ID | <VI1PR01MB5344AF95363EDD2F9FC2B26CD72F0@VI1PR01MB5344.eurprd01.prod.exchangelabs.com> |
Excellent, I can work with that. Great starting point example and suggestions! Thanks Maurizio ________________________________ From: Maurizio Berti <[email protected]> Sent: Thursday, April 11, 2019 10:10 PM To: Matic Kukovec Cc: [email protected] Subject: Re: [PyQt] QTabWidget tab movement? This should do the trick: class TabWidget(QtWidgets.QTabWidget): moveRange = None def setMovable(self, movable): if movable == self.isMovable(): return QtWidgets.QTabWidget.setMovable(self, movable) if movable: self.tabBar().installEventFilter(self) else: self.tabBar().removeEventFilter(self) def eventFilter(self, source, event): if source == self.tabBar(): if event.type() == QtCore.QEvent.MouseButtonPress and event.buttons() == QtCore.Qt.LeftButton: QtCore.QTimer.singleShot(0, self.setMoveRange) elif event.type() == QtCore.QEvent.MouseButtonRelease: self.moveRange = None elif event.type() == QtCore.QEvent.MouseMove and self.moveRange is not None: if event.x() < self.moveRange[0] or event.x() > self.tabBar().width() - self.moveRange[1]: return True return QtWidgets.QTabWidget.eventFilter(self, source, event) def setMoveRange(self): tabRect = self.tabBar().tabRect(self.currentIndex()) pos = self.tabBar().mapFromGlobal(QtGui.QCursor.pos()) self.moveRange = pos.x() - tabRect.left(), tabRect.right() - pos.x() The delayed setMoveRange() is required since the position of the tab might change if you have tabs that would take more space than available for the QTabBar and the clicked tab is not fully visible. Keep also in mind that, if that's the case, it might create some issues while moving tabs beyond the limit. Also, if you move the mouse too fast beyond the limit, the selected bar will be "stuck" according to the last accepted mouseMoveEvent. Maybe this could be fixed by sending a fake QMouseEvent, but I'm not that sure it would make things "easier" (you should ensure that the tabRect is not yet at the edge of the QTabBar before). To avoid all that you should probably work harder by subclassing your own QTabBar and then implement mouseMoveEvent all by yourself, but this would also require some very difficult management of the paintEvent too. Also, remember that I didn't take into account margins added by styles, nor the possibility of cornerWidget(s). Maurizio Il giorno gio 11 apr 2019 alle ore 21:23 Matic Kukovec <[email protected]<mailto:[email protected]>> ha scritto: Hi, Is there a way to change the behavior and animation of a QTabWidgets tab bar drag functionality? [cid:16a0dfa0c4264cee6f1] In the above image you can see that the dragged tab disappears outside the bounds of the tab bar. I would like to remove the movement of tabs outside the tab bar space. Can anyone suggest of where to begin in changing this behavior? Thanks, Matic _______________________________________________ PyQt mailing list [email protected]<mailto:[email protected]> https://www.riverbankcomputing.com/mailman/listinfo/pyqt -- È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi http://www.jidesk.net _______________________________________________ PyQt mailing list [email protected] https://www.riverbankcomputing.com/mailman/listinfo/pyqt
tab_movement.gif
(image/gif, 43 KB) - not displayed