简体   繁体   English

PyQt4工具栏按钮对齐

[英]PyQt4 Toolbar Button Alignment

I have this code 我有这个代码

    Menu = self.menuBar()
    EditMenu = Menu.addMenu("&File")
    OptionMenu = Menu.addMenu("&Options")
    HelpMenu = Menu.addMenu("&Help")
    EditMenu.addActions((fileNewAction,faultAction,storeAction,localAction,scheduleAction))
    OptionMenu.addAction(settingAction)

    Toolbar = QtGui.QToolBar()
    Toolbar.setIconSize(QtCore.QSize(50,50))
    Toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon|QtCore.Qt.AlignLeading) #<= Toolbuttonstyle
    self.addToolBar(QtCore.Qt.LeftToolBarArea,Toolbar)
    Toolbar.addActions((fileNewAction,faultAction,scheduleAction,storeAction,localAction,settingAction))
    settings = QtCore.QSettings()
    self.restoreGeometry(settings.value("Geometry").toByteArray())

which give me this 给我这个

在此处输入图片说明

i used 我用了

Toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon|QtCore.Qt.AlignLeading)

to display the text on the right side of the toolbar button and to align all the toolbar button images to the left. 在工具栏按钮的右侧显示文本,并将所有工具栏按钮的图像向左对齐。 But the texts are not appearing on the right side. 但是这些文本没有出现在右侧。

If i remove the QtCore.Qt.AlignLeading , 如果我删除了QtCore.Qt.AlignLeading

I get unaligned (left side) buttons like this 我得到这样的未对齐(左侧)按钮

在此处输入图片说明

(1) How do i get my toolbar button icons align to the left and display the text on the right side at the same time? (1)如何使工具栏按钮图标左对齐并同时在右侧显示文本?

Another question is 另一个问题是

(2) How do i adjust the width of the raised button effect when i mouse over on each button? (2)当我将鼠标悬停在每个按钮上时,如何调整raised button效果的宽度? I want the width of all the buttons to be the same. 我希望所有按钮的宽度都相同。 The width seems to be different depending on how long the text is. 宽度似乎有所不同,具体取决于文本的长度。

在此处输入图片说明在此处输入图片说明

IMHO, the problem is the QToolBar decides on a size of each button individually, disregarding neighboring buttons completely. 恕我直言,问题是QToolBar单独决定每个按钮的大小,而完全忽略相邻按钮。 But you can set the size manually: 但是您可以手动设置大小:

for action in my_toolbar.actions():
    widget = my_toolbar.widgetForAction(action)
    widget.setFixedSize(width, height)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM