Python/QT - 我该怎么做才能在一系列小部件之间获得 4 个像素的边框?
Python/QT - What can I do to get 4 pixel borders between series of widgets?
我查看了 PySide 和 QT 文档,并进行了谷歌搜索,但尚未找到解决方案。
在这个 Python 2.7/Qt4.8 (PySide) 示例中,我有一系列通过 CSS 具有 4 像素圆角边框的小部件。我试图做的是将这些小部件彼此相邻放置,并使小部件之间的边框与其他小部件的厚度相同。在我的例子中,从技术上讲,QT 完全按照我的要求去做,但我需要一些特殊的东西,这样我最终在每个小部件之间有 4 px 的边界,而不是 8 (4 x 2)。
这是我得到的结果。
这是我需要的(Photoshopped)
这里是示例代码。
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 像素的边框?
您可以尝试调整边距:
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)
我查看了 PySide 和 QT 文档,并进行了谷歌搜索,但尚未找到解决方案。
在这个 Python 2.7/Qt4.8 (PySide) 示例中,我有一系列通过 CSS 具有 4 像素圆角边框的小部件。我试图做的是将这些小部件彼此相邻放置,并使小部件之间的边框与其他小部件的厚度相同。在我的例子中,从技术上讲,QT 完全按照我的要求去做,但我需要一些特殊的东西,这样我最终在每个小部件之间有 4 px 的边界,而不是 8 (4 x 2)。
这是我得到的结果。
这是我需要的(Photoshopped)
这里是示例代码。
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 像素的边框?
您可以尝试调整边距:
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)