I was hoping someone could help me with creating a custom title bar widget for a dock widget in a PyQt4 GUI program. All I want to do is emulate the exact same look and function of the default title bar, but with an extra, custom button. I couldn't find an easy way to do this as I don't know if there's a default title bar widget I can add stuff to, so I made a custom dock title bar widget:
from PyQt4 import QtGui, QtCore
class DockTitleBar(QtGui.QFrame):
def __init__(self, parent):
super(DockTitleBar, self).__init__(parent)
# Is this the only way to give the title bar a border?
self.setFrameStyle(QtGui.QFrame.Raised | QtGui.QFrame.StyledPanel)
# Layout for title box
layout = QtGui.QHBoxLayout(self)
layout.setSpacing(1)
layout.setMargin(1)
self.label = QtGui.QLabel(parent.windowTitle())
icon_size = QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_TitleBarNormalButton).actualSize(
QtCore.QSize(100, 100))
button_size = icon_size + QtCore.QSize(5, 5)
# Custom button I want to add
self.button = QtGui.QToolButton(self)
self.button.setAutoRaise(True)
self.button.setMaximumSize(button_size)
self.button.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_TitleBarContextHelpButton))
self.button.clicked.connect(self.do_something)
# Close dock button
self.close_button = QtGui.QToolButton(self)
self.close_button.setAutoRaise(True)
self.close_button.setMaximumSize(button_size)
self.close_button.setIcon(QtGui.QApplication.style().standardIcon(
QtGui.QStyle.SP_DockWidgetCloseButton))
self.close_button.clicked.connect(self.close_parent)
# Setup layout
layout.addWidget(self.label)
layout.addStretch()
layout.addWidget(self.button)
layout.addWidget(self.close_button)
def do_something(self):
# Do something when custom button is pressed
pass
def close_parent(self):
self.parent().hide()
It seems to work okay, except for when the dock is dragged around in its floating state. Normally there are borders and even the title bar is highlighted, but with my janky version there is no frame for the floating dock so it's hard to tell where it is, and the title bar isn't highlighted. Is there something I can fix/add or should I be doing this in an entirely different way?