我有一个qtabwidget在任何给定时间都包含一个或多个选项卡。 我希望用户能够以任意顺序重新排列第二个到最后一个选项卡,但第一个选项卡保留在索引 0 处。
从我发现的所有内容来看,没有办法启用 setMovable每个选项卡独立。
到目前为止,我想出的最佳解决方法是,如果用户 move 第一个选项卡(或其前面的另一个选项卡),则只需将第一个选项卡移回位置 0。这显然不理想,但如果它工作正常的话是可以接受的...它可以工作很短一段时间,但是它偶尔会使应用程序崩溃(当用户在其前面拖动选项卡并将鼠标按住在那里时,似乎会发生这种情况,所以它处于不断尝试重新排列的循环中,并且某些低级别的崩溃)
关于可行的解决方法还有其他建议吗(无论是此方法,还是更容易实现此功能的类似小部件)?我认为可能可以以忽略第一个选项卡上的鼠标拖动的方式重新分类 QTabWidget,但我不确定如何防止另一个选项卡在它之前 move ...
最佳答案
我发现的唯一方法是“固定” QTabWidget 的第一个选项卡是通过使用 QTabBar 的子类。全局策略包括在QTabBar
的子类上安装eventFilter
并有条件地阻止MouseMove
事件,以便:
- 将第一个选项卡的位置固定在索引 0 处;
- 防止其他选项卡的左边缘 move 到第一个选项卡右边缘的左侧。这可以防止其他选项卡 move 到第一个选项卡的前面。
下面的代码展示了一个简单的应用程序来展示如何完成此操作。
import sys
from PySide import QtGui, QtCore
class myQTabBar(QtGui.QTabBar):
def __init__(self, *args, **kargs):
super(myQTabBar, self).__init__(*args, **kargs)
self.setMovable(True)
self.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.Type.MouseMove:
if source.currentIndex() == 0: # Block MouseMove for first tab.
return True
else: # For remaining tabs:
# block MouseMove if the left edge of the moving tab goes
# farther to the left than the right edge of first tab.
moving_leftEdge = event.pos().x() - self.edge_offset
fixed_rightEdge = self.tabRect(0).width()
if moving_leftEdge < fixed_rightEdge:
return True
elif event.type() == QtCore.QEvent.Type.MouseButtonPress:
# Get mouse click horizontal position.
xclick = event.pos().x()
# Get the left edge horizontal position of the targeted tab.
xleft = self.tabRect(self.tabAt(event.pos())).x()
# Compute and store offset between mouse click horizontal
# position and the left edge of the targeted tab
self.edge_offset = xclick - xleft
return QtGui.QWidget.eventFilter(self, source, event)
class myQTabWidget(QtGui.QTabWidget):
def __init__(self, *args, **kargs):
super(myQTabWidget, self).__init__(*args, **kargs)
tab_bar = myQTabBar()
self.setTabBar(tab_bar)
self.addTab(QtGui.QWidget(), 'Tab1')
self.addTab(QtGui.QWidget(), 'Tab2')
self.addTab(QtGui.QWidget(), 'Tab3')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
instance1 = myQTabWidget()
instance1.show()
sys.exit(app.exec_())
结果是:
关于python - PySide Qt4 - Qtabwidget - 禁用单个 q 选项卡小部件的拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31845768/