[PyQt] QTabWidget tab movement?
Maurizio Berti
maurizio.berti at gmail.com
Thu Apr 11 21:10:59 BST 2019
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 <
kukovecmatic at hotmail.com> ha scritto:
> Hi,
>
> Is there a way to change the behavior and animation of a QTabWidgets tab
> bar drag functionality?
> 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 PyQt at riverbankcomputing.com
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190411/c259a789/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tab_movement.gif
Type: image/gif
Size: 43989 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190411/c259a789/attachment-0001.gif>
More information about the PyQt
mailing list