3

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_() 
Community
  • 1
  • 1
Jay
  • 3,373
  • 6
  • 38
  • 55

1 Answers1

4

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_())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks, I tried that before posting this question and now again and raise_() doesn't work. I've added my code incase it helps... – Jay Oct 19 '11 at 06:30
  • @mushu. I've added an example to my answer which works for me. – ekhumoro Oct 19 '11 at 12:42
  • Thanks but I need to raise_() straight after the docks have been created. I tried your method and yes it did work but not when I call "self.handleButton()" right after creating the docks. I don't want to have to add a button to raise docks as the user could just click the dock they want instead of clicking the button. – Jay Oct 20 '11 at 07:22
  • My example is just a demo - I'm not suggesting you use a button to change tabs! But anyway, I've changed my example slightly to show how the initial tab could be selected. Obviously, in your own program, you will need different code to determine which dock should be selected first (and then simply call `raise_()` on it). – ekhumoro Oct 20 '11 at 13:12
  • In my original code I have the self.docklist[0].raise_(). Even though your example does work the raise_() function in my code does not. The fact that raise_() is not working for me is the basis of my question. If you look at my code you will see that it already has your suggested "self.dockList[0].raise_()". Thanks for your examples but again, I know how the raise_() function is supposed to work. My problem is that in my case it doesn't. – Jay Oct 24 '11 at 07:26
  • 1
    @Jay (I realize this is 12 years later), the issue is you hit the .raise_() too quickly so the app loop hits a race condition and fails to do the raise. Try calling the rase_ in a QtCore.QTimer.singleShot with a small (or even 0) time delay. – Brian Feb 20 '23 at 22:02