移动QToolBar图标?

2024-10-01 17:26:13 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法移动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)

Tags: self图标toolbar办法qtguiqtcoremainwindowqsize
2条回答

使用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。在

您可以添加间隔垫圈:

#!/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_())

相关问题 更多 >

    热门问题