1

有没有办法在 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)
4

2 回答 2

2

You could add a spacer:

#!/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_())
于 2013-09-24T04:59:15.223 回答
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。

于 2013-09-23T01:32:44.873 回答