2017-08-11 27 views
1

我已經看過PySide和QT文檔,並且使用了搜索引擎優化功能,但還沒有找到解決方案。Python/QT - 我能做些什麼來獲得系列組件之間的4個像素邊界?

在這個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邊框?

+0

佈局中的間距如何? – Trilarion

回答

2

您可以嘗試調整頁邊距:

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) 
+0

差不多!圓角部分從CSS邊距屬性的裁剪效果略微切碎,我認爲您在將所述屬性設置爲-2時注意到了這一點。 –

+0

@PaulW,您是如何在測試中選擇2個頂級小部件的,是否有CSS選擇器或您是否命名了小部件?一個問題的答案:p – PRMoureu

+0

@PRMourea,在Python中,我將setProperty('nonbottom',True)用於前2個小部件,然後在CSS中通過*選擇所述小部件* [nonbottom =「true」] {bla bla bla ;}。 –

相關問題