使用 QToolButton。
def create_toolbutton(parent, text = None, shortcut = None,
icon = None, tip = None, toggled = None,
triggered = None, autoraise = True,
text_beside_icon = False):
''' create an toolbutton '''
button = QToolButton(parent)
if text is not None:
button.setText(text)
if icon is not None:
icon = getIcon(icon)
button.setIcon(icon)
if text is not None or tip is not None:
button.setToolTip(text if tip is None else tip)
if text_beside_icon:
button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
button.setAutoRaise(autoraise)
if triggered is not None:
QObject.connect(button,SIGNAL('clicked()'), triggered)
if toggled is not None:
QObject.connect(button,SIGNAL('toggled(bool)'), toggled)
button.setCheckable(True)
if shortcut is not None:
button.setShortcut(shortcut)
return button
exit_btn = create_toolbutton(self.dockWidget, 'Exit', 'Ctrl+Q',QtGUI.QIcon('exit34.png'),'Exit the app', QtGui.qApp.quit )
btn_layout = QHBoxLayout()
btn_layout.addWidget(exit_btn)
btn_layout.setAlignment(Qt.AlignRight)
layout = QVBoxLayout()
layout.addLayout(btn_layout)
layout.addWidget( your_main_widget )
然后将布局设置为dockwidget。