Python/QT - 我该怎么做才能在一系列小部件之间获得 4 个像素边框?

标签 python qt border pyside

我查看了 PySide 和 QT 文档,并用谷歌搜索,但尚未找到解决方案。

在这个 Python 2.7/Qt4.8 (PySide) 示例中,我有一系列通过 CSS 具有 4 px 圆形边框的小部件。我试图做的是将这些小部件彼此相邻放置,并使小部件之间的边框与其余部件的厚度相同。就我而言,从技术上讲,QT 所做的正是我所要求的,但我需要一些特殊的东西,以便最终每个小部件之间有 4 px 边框,而不是 8 (4 x 2)。

这就是我得到的。

enter image description here

这是我需要的(经过Photoshop处理)

enter image description here

这是示例代码。

from PySide.QtGui import *
import sys

css = '''
QLabel{
    border: 4px solid #555555;
    border-radius: 6px;
}
'''

class MainWidget(QWidget):

    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.setStyleSheet(css)
        layout = QVBoxLayout(self)
        layout.setSpacing(0)

        for i in range(3):
            label = QLabel('hello')
            layout.addWidget(label)

app = QApplication([])
win = MainWidget()
win.show()
sys.exit(app.exec_())

我尝试过简单地不渲染顶部 2 个小部件的底部边框,这让我达到了目标,但解决方案是半丑陋的,并且不起作用。

如何才能在这些小部件之间获得 4 px 边框?

最佳答案

您可以尝试调整边距:

css = '''

QLabel{
    border: 4px solid #555555;
    margin-top: -1;
    margin-bottom: -1;
    border-radius: 6px;
}
'''

(我先尝试使用-2,但偏移量导致了不好的效果)

编辑:

以下方法可以更准确地控制边距,而不会出现难看的裁剪:

css = '''
QLabel{
    border: 4px solid #555555;
    border-radius: 6px;

}
*[nobottom="true"]{
    margin-bottom: -2;    

}
*[notopnobottom="true"]{
    margin-top: -2;
    margin-bottom: -2;    

}
*[notop="true"]{
    margin-top: -2;
}
'''

class MainWidget(QWidget):

    def __init__(self, parent=None):
        super(MainWidget, self).__init__(parent)
        self.setStyleSheet(css)
        layout = QVBoxLayout(self)
        layout.setSpacing(0)
        labeltop = QLabel('hello')
        labeltop.setProperty('nobottom', True)
        labelmid = QLabel('hello')
        labelmid.setProperty('notopnobottom', True)
        labelbottom = QLabel('hello')
        labelbottom.setProperty('notop', True)
        layout.addWidget(labeltop)
        layout.addWidget(labelmid)
        layout.addWidget(labelbottom)

关于Python/QT - 我该怎么做才能在一系列小部件之间获得 4 个像素边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45642204/

相关文章:

html - 透明边框和框阴影问题

python - 如何使用两个不同的电子邮件地址发送电子邮件?

php - Python 相当于 PHPs __call() 魔术方法?

python - Django SSL Redirect 片段修改,未按预期工作

c++ - QXcbIntegration : Cannot create platform OpenGL context, GLX 和 EGL 均未启用

java - 不可编辑的 JComboBox 边框

python - 如何设置 Pandas 提取某些列的某些行并将它们堆叠在一起?

c++ - 与 PushButton 关联的 QLabel 不出现

c++ - QPainterPath 文本在打印时呈现错误

ios - 显示边界外的图像 - iOS/Swift