python - 如何控制 QToolBar 中的 QAction 按钮间距?

标签 python qt pyqt pyside qtoolbar

enter image description here

将四个 QAction 按钮添加到 QToolBar 后,需要设置哪些小部件属性以及设置什么值才能使按钮之间没有间距。所以每个按钮并排放置? 从下面发布的示例可以看出,我尝试通过以下方式实现零间距:

    toolbar.setContentsMargins(0, 0, 0, 0)
    toolbar.layout().setSpacing(0)
    toolbar.layout().setContentsMargins(0, 0, 0, 0)

但这没有什么区别,按钮仍然彼此间隔......

import sys
from PyQt4.QtGui import *

class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()

        self.initUI()

    def initUI(self):               

        textEdit = QTextEdit()
        self.setCentralWidget(textEdit)

        btn1 = QAction(QIcon('icons/btn1.png'), 'Button 01', self)
        btn2 = QAction(QIcon('icons/btn2.png'), 'Button 02', self)
        btn3 = QAction(QIcon('icons/btn3.png'), 'Button 03', self)
        btn3.setEnabled(False)

        btn1.setShortcut('Ctrl+Q')
        btn1.triggered.connect(self.close)

        toolbar = self.addToolBar('Exit')
        toolbar.addAction(btn1)
        toolbar.addAction(btn2)
        toolbar.addAction(btn3)
        toolbar.addSeparator()

        toolbar.setContentsMargins(0, 0, 0, 0)
        toolbar.layout().setSpacing(0)
        toolbar.layout().setContentsMargins(0, 0, 0, 0)


        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('Main window')    
        self.show()

def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main() 

最佳答案

来自 QToolBar 的样式表示例:

spacing: 3px; /* spacing between items in the tool bar */

所以这应该可以解决问题:

toolbar.setStyleSheet("QToolBar{spacing:0px;}");

关于python - 如何控制 QToolBar 中的 QAction 按钮间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007589/

相关文章:

c++ - 如何使用 Qt c++ QGeoServiceProvider 从地理地址获取纬度/经度?

qt - 在 QTabWidget 中访问选项卡的小部件

python - 隐藏 QFileSystemModel 中的大小、类型和修改日期列

python - setup_method 中的 py.test session 级固定装置

python - Windows 上的 IPython - 没有突出显示或自动完成

python - 如何从文本文件中的行和列中输出无效有效数字的位置

c++ - 为什么 QWizard::nextId() 被调用两次?

python - QTableWidget:插入行使应用程序崩溃 python

python - QTableWidget 中的从属 QComboBox

python - 加权平均 Pandas