有没有办法在QToolBar中移动图标?我想把它们移到最右边。我把它设置成这样:
self.dockWidget = QtGui.QDockWidget(MainWindow)
self.dockWidget.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures).
self.toolbar = QtGui.QToolBar()
self.toolbar.setIconSize(QtCore.QSize(10, 10))
exitAction = QtGui.QAction(QtGui.QIcon('exit34.png'), 'Exit', self.dockWidget)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(QtGui.qApp.quit).
self.toolbar.addAction(exitAction)
self.dockWidget.setTitleBarWidget(self.toolbar)
答案 0 :(得分:2)
您可以添加间隔符:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#---------
# IMPORT
#---------
import sys
from PyQt4 import QtGui, QtCore
#---------
# DEFINE
#---------
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.toolBar = QtGui.QToolBar(self)
self.addToolBar(QtCore.Qt.ToolBarArea(QtCore.Qt.TopToolBarArea), self.toolBar)
self.spacer = QtGui.QWidget()
self.spacer.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
self.actionLeft = QtGui.QAction(self)
self.actionLeft.setIcon(QtGui.QIcon.fromTheme("media-seek-backward"))
self.actionRight = QtGui.QAction(self)
self.actionRight.setIcon(QtGui.QIcon.fromTheme("media-seek-forward"))
self.toolBar.addAction(self.actionLeft)
self.toolBar.addWidget(self.spacer)
self.toolBar.addAction(self.actionRight)
#---------
# MAIN
#---------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.resize(333, 111)
main.show()
sys.exit(app.exec_())
答案 1 :(得分:1)
使用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。