我查看了 PySide 和 QT 文档,并用谷歌搜索,但尚未找到解决方案。
在这个 Python 2.7/Qt4.8 (PySide) 示例中,我有一系列通过 CSS 具有 4 px 圆形边框的小部件。我试图做的是将这些小部件彼此相邻放置,并使小部件之间的边框与其余部件的厚度相同。就我而言,从技术上讲,QT 所做的正是我所要求的,但我需要一些特殊的东西,以便最终每个小部件之间有 4 px 边框,而不是 8 (4 x 2)。
这就是我得到的。
这是我需要的(经过Photoshop处理)
这是示例代码。
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/