2013-09-21 56 views
1

有沒有辦法在QToolBar中移動圖標?我想把他們移到最右邊。我有這樣的設置:pyqt:移動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

你有它現在的樣子的截圖,和一個模擬了你想怎麼看?你只是想在右邊的工具欄?或者一個圖標作爲工具欄中最右邊的選項? – 2013-09-23 01:32:38

回答

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。

1

你可以添加一個間隔:

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