Jay
Jay

Reputation: 3489

Set top QDockWidget for a bunch of tabbified QDockWidgets

H there.

Can someone please tell me how to set a tabbified QDockWidget to pop to the front (be the active dock)?

In the picture below, the "full" tab is selected and it's contents are visible but I want to set the "mouth" tab to the selected tab and have its contents visible.

tabs

Code:

self.dockList = []
approvedAdded = False
# add new dock widgets
for dockName in previewDict.keys():
    previewList = previewDict[ dockName ]
    # setup dock
    dock = QDockWidget( dockName )
    dock.setWidget( PreviewWidget( previewList ) )
    dock.setAllowedAreas( Qt.TopDockWidgetArea )
    dock.setFeatures( QDockWidget.DockWidgetMovable | QDockWidget.DockWidgetFloatable )

    # add to ui
    self.addDockWidget( Qt.TopDockWidgetArea , dock )
    
    # add to list
    insertIndex = len( self.dockList ) - 1
    if dockName == "approved":
        insertIndex = 0
        approvedAdded = True
    elif dockName == tfPaths.user():
        if not approvedAdded:
            insertIndex = 0
        else:
            insertIndex = 1
            
    self.dockList.insert( insertIndex , dock )
  

# tabify dock widgets
if len( self.dockList ) > 1:
    for index in range( 0 , len(self.dockList) - 1 ):
        self.tabifyDockWidget( self.dockList[index] , self.dockList[index + 1] )

# set tab at pos [0] in list to active
if self.dockList:
    print self.dockList[0].windowTitle()
    self.dockList[0].raise_() 

Upvotes: 3

Views: 5604

Answers (1)

ekhumoro
ekhumoro

Reputation: 120778

A tabified dockwidget can be set as the selected tab like this:

dockwidget.raise_()

EDIT

Here's a runnable example based on the code in the question:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('Dock Widgets')
        self.button = QtGui.QPushButton('Raise Next Tab', self)
        self.button.clicked.connect(self.handleButton)
        self.setCentralWidget(self.button)
        self.dockList = []
        approvedAdded = False
        for dockName in 'Red Green Yellow Blue'.split():
            dock = QtGui.QDockWidget(dockName)
            dock.setWidget(QtGui.QListWidget())
            dock.setAllowedAreas(QtCore.Qt.TopDockWidgetArea)
            dock.setFeatures(QtGui.QDockWidget.DockWidgetMovable |
                             QtGui.QDockWidget.DockWidgetFloatable)
            self.addDockWidget(QtCore.Qt.TopDockWidgetArea, dock)
            insertIndex = len(self.dockList) - 1
            if dockName == 'Green':
                insertIndex = 0
                approvedAdded = True
            elif dockName == 'Yellow':
                if not approvedAdded:
                    insertIndex = 0
                else:
                    insertIndex = 1
            self.dockList.insert(insertIndex, dock)
        if len(self.dockList) > 1:
            for index in range(0, len(self.dockList) - 1):
                self.tabifyDockWidget(self.dockList[index],
                                      self.dockList[index + 1])
        self.dockList[0].raise_()
        self.nextindex = 1

    def handleButton(self):
        self.dockList[self.nextindex].raise_()
        self.nextindex += 1
        if self.nextindex > len(self.dockList) - 1:
            self.nextindex = 0

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Upvotes: 4

Related Questions