无法从 QVBoxLayout 中删除边距 [英] Can't remove margins from QVBoxLayout

查看:68
本文介绍了无法从 QVBoxLayout 中删除边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个带有一些文本的图像按钮,但由于一些额外的边距,图像与文本不对齐.我怎样才能摆脱这些边距?我已经尝试在各种组件上设置 setContentsMargins(0,0,0,0)setSpacing(0) 但它似乎不会影响正确的边距.>

这是一个演示:

 from PyQt5.QtGui import *从 PyQt5.QtWidgets 导入 *从 PyQt5.QtCore 导入 *类 ImageButton(QWidget):def __init__(self, img_location):QWidget.__init__(self)self.img_location = img_locationself.button = QToolButton(self)self.button.clicked.connect(self.handleButton)self.button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)self.button.setIcon(QIcon(img_location))self.button.setIconSize(QSize(300,400))布局 = QVBoxLayout(self)layout.addWidget(self.button)def handleButton(self):打印(self.img_location)app = QApplication([])窗口 = QMainWindow()window.resize(800,600)label_title = QLabel("bob asdfjak f ajksf asljf ajdslf aldskj ksf kslfhadjks lfhiu sofhjaklfsiuod fahklfhadisufaksufhdsuifhosa fasdf afsda")label_title.setStyleSheet('背景色:黄色')label_title.setAlignment(Qt.AlignCenter)label_title.adjustSize()label_title.setWordWrap(True)imgbtn = ImageButton("a.png")imgbtn.setStyleSheet('背景色:绿色')layout_box = QVBoxLayout()layout_box.setContentsMargins(0,0,0,0)layout_box.setSpacing(0)layout_box.addWidget(imgbtn, 0, Qt.AlignTop)layout_box.addWidget(label_title, 0, Qt.AlignTop)layout_box.setAlignment(Qt.AlignCenter)layout_box.setSpacing(0)内容 = QWidget()content.setStyleSheet('背景颜色:蓝色')content.setFixedWidth(300)content.setFixedHeight(400)content.setLayout(layout_box)window.setCentralWidget(内容)window.show()app.exec_()

黄色区域标记文本标签,绿色区域标记图像按钮,蓝色区域标记我试图摆脱的空间.看看黄色区域如何扩展到蓝色区域的大小,最终导致文本与图像按钮不对齐.

我怎样才能摆脱这个蓝色区域?

解决方案

该边距是用于将 QToolButton 放置在 ImageButton 内的 QVBoxLayout 的边距,因此解决方案是将其设置为 0 边距:

# ...类 ImageButton(QWidget):def __init__(self, img_location):super().__init__(self)# ...布局 = QVBoxLayout(self)layout.addWidget(self.button)layout.setContentsMargins(0, 0, 0, 0)# ...

I have set up an image button with some text, but the image does not align with the text due to some extra margins. How can I get rid of these margins? I have tried setting setContentsMargins(0,0,0,0) and setSpacing(0) on various components but it doesn't seem to affect the correct margins.

Here is a demo:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class ImageButton(QWidget):
    def __init__(self, img_location):
        QWidget.__init__(self)
        self.img_location = img_location

        self.button = QToolButton(self)
        self.button.clicked.connect(self.handleButton)
        self.button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.button.setIcon(QIcon(img_location))
        self.button.setIconSize(QSize(300,400))
        layout = QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        print(self.img_location)


app = QApplication([])
window = QMainWindow()
window.resize(800,600)



label_title = QLabel("bob asdfjak f ajksf asljf ajdslf aldskj ksf kslfhadjks lfhiu sofhjaklfsiuod fahklfhadisufaksufhdsuifhosa fasdf afsda")
label_title.setStyleSheet('background-color: yellow')
label_title.setAlignment(Qt.AlignCenter)
label_title.adjustSize()
label_title.setWordWrap(True)

imgbtn = ImageButton("a.png")
imgbtn.setStyleSheet('background-color: green')

layout_box = QVBoxLayout()
layout_box.setContentsMargins(0,0,0,0)
layout_box.setSpacing(0)


layout_box.addWidget(imgbtn, 0, Qt.AlignTop)
layout_box.addWidget(label_title, 0, Qt.AlignTop)
layout_box.setAlignment(Qt.AlignCenter)
layout_box.setSpacing(0)

content = QWidget()
content.setStyleSheet('background-color: blue')
content.setFixedWidth(300)
content.setFixedHeight(400)
content.setLayout(layout_box)

window.setCentralWidget(content)
window.show()
app.exec_()

The Yellow region marks the text label, the Green region marks the imagebutton, and the Blue region marks the space I'm trying to get rid of. See how the yellow region expands to the size of the blue, with the end result that the text doesn't align to the imagebutton.

How can I get rid of this blue region?

解决方案

That margin is that of the QVBoxLayout that you use to place the QToolButton inside ImageButton, so the solution is to set it to 0 that margin:

# ...
class ImageButton(QWidget):
    def __init__(self, img_location):
        super().__init__(self)
        # ...
        layout = QVBoxLayout(self)
        layout.addWidget(self.button)
        layout.setContentsMargins(0, 0, 0, 0)
    # ...

这篇关于无法从 QVBoxLayout 中删除边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆