Frage

I'm trying to make a tab widget with a custom tab bar movable. The program works fine if I don't subclass QTabBar and setMovable(True) but I need to access the mouseDoubleClickEvent that QTabBar offers thus the sub classing. When you run the program everything works until the point you try and move a tab with the mouse. I feel like I've tried every possible combination of setMovable I can think of but nothing's working. Am I doing something wrong?

using:

python v2.7.2

PyQt4 v4.8.5

and disgusting Windows XP

from PyQt4.QtGui  import *
from PyQt4.QtCore import *

import sys


class Main(QWidget):

    def __init__(self, parent=None):

        super(Main, self).__init__(parent)
        self.widgetBox = QHBoxLayout(self)
        self.tabs = CTabWidget()
        self.widgetBox.addWidget(self.tabs)
        self.setLayout(self.widgetBox)


class CTabWidget(QTabWidget):

    def __init__(self, parent=None):

        super(CTabWidget, self).__init__(parent)

        self.tabBar = CTabBar(self)
        self.tabBar.addTab("Foo")
        self.tabBar.addTab("Bar")

        self.setTabBar(self.tabBar)

        self.setTabPosition(QTabWidget.West)
        self.setMovable(True)

class CTabBar(QTabBar):

    def __init__(self, parent=None):
        super(CTabBar, self).__init__(parent)
        self.setMovable(True)

    def addTab(self, string):
        super(CTabBar, self).addTab(QString(string))

    def mouseDoubleClickEvent(self, event):
        print "Change name"


class Run(object):

    def __init__(self):
        app = QApplication(sys.argv)
        app.setStyle(QStyleFactory.create("plastique"))
        main = Main()
        main.show()
        sys.exit(app.exec_())


Run()
War es hilfreich?

Lösung

Use the QTabWidget.addTab method to add tabs, and make sure you associate a widget with each tab:

class CTabWidget(QTabWidget):
    def __init__(self, parent=None):
        super(CTabWidget, self).__init__(parent)
        self.tabBar = CTabBar(self)
        self.setTabBar(self.tabBar)
        self.setTabPosition(QTabWidget.West)
        self.tab1 = QWidget(self)
        self.tab2 = QWidget(self)
        self.addTab(self.tab1, "Foo")
        self.addTab(self.tab2, "Bar")

class CTabBar(QTabBar):
    def __init__(self, parent=None):
        super(CTabBar, self).__init__(parent)
        self.setMovable(True)

    def mouseDoubleClickEvent(self, event):
        print "Change name"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top