[PyQt] QTabWidget tab movement?

Matic Kukovec kukovecmatic at hotmail.com
Thu Apr 11 21:55:24 BST 2019


Excellent, I can work with that.
Great starting point example and suggestions!

Thanks Maurizio
________________________________
From: Maurizio Berti <maurizio.berti at gmail.com>
Sent: Thursday, April 11, 2019 10:10 PM
To: Matic Kukovec
Cc: pyqt at riverbankcomputing.com
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 <kukovecmatic at hotmail.com<mailto:kukovecmatic at hotmail.com>> 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    PyQt at riverbankcomputing.com<mailto: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/c3d6fb96/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tab_movement.gif
Type: image/gif
Size: 43989 bytes
Desc: tab_movement.gif
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190411/c3d6fb96/attachment-0001.gif>


More information about the PyQt mailing list