2017-04-22 66 views
0

的左侧有一些小部件水平居中:窗口小部件水平和标签集中到这些小部件

class TestWidget(QWidget): 

    def __init__(self): 
     super().__init__() 
     self._init_ui() 

    def _init_ui(self): 
     edit1_label = QLabel("Edit1") 
     edit2_label = QLabel("Edit2") 
     edit3_label = QLabel("Edit3") 
     edit1 = QLineEdit() 
     edit2 = QLineEdit() 
     edit3 = QLineEdit() 

     layout = QVBoxLayout() 
     layout.addWidget(edit1) 
     layout.addWidget(edit2) 
     layout.addWidget(edit3) 
     layout.addStretch() 

     hlayout = QHBoxLayout() 
     hlayout.addLayout(layout) 
     self.setLayout(hlayout) 
     self.setAttribute(Qt.WA_StyledBackground) 


def _main(): 
    app = QApplication(['']) 
    app.setStyleSheet(""" 
        TestWidget { 
         min-height: 400px; 
         max-height: 400px; 
         min-width: 600px; 
         max-width: 600px; 
        } 
        TestWidget QLineEdit { 
         min-height: 2em; 
         max-height: 2em; 
         min-width: 20em; 
         max-width: 20em; 
         } 
        """) 
w = TestWidget() 
w.show() 
app.exec_() 

我想标签(edit1_label,edit2_label,edit3_label)的地方进行编辑的左(EDIT1, edit2,edit3)。但编辑应该在表单上水平居中。它如何实施?

更新:新增例如图形(请注意QLineEdits都在表格上水平居中):

example graphic

的错误结果: enter image description here

+0

总之,你想把QLabels放在QLineEdits的一面wi而不是分散他们 – eyllanesc

+0

是的,你是对的,@eyllanesc – Aleksey

+0

你可能想看看QFormLayout。 – HashSplat

回答

1

我不知道这是否是你想要的,但它可能有所帮助:

def _init_ui(self): 
    mainLayout = QGridLayout() 
    mainLayout.setColumnStretch(0, 1) 
    mainLayout.setColumnStretch(3, 1) 

    edit1_label = QLabel("Edit1") 
    edit1 = QLineEdit() 

    mainLayout.addWidget(edit1_label, 1, 1) 
    mainLayout.addWidget(edit1, 1, 2) 

    edit2_label = QLabel("Edit2") 
    edit2 = QLineEdit() 

    mainLayout.addWidget(edit2_label, 2, 1) 
    mainLayout.addWidget(edit2, 2, 2) 

    edit3_label = QLabel("Edit3") 
    edit3 = QLineEdit() 

    mainLayout.addWidget(edit3_label, 3, 1) 
    mainLayout.addWidget(edit3, 3, 2) 

    mainLayout.setAlignment(Qt.AlignTop) 

    self.setLayout(mainLayout) 
    self.setAttribute(Qt.WA_StyledBackground) 
+0

谢谢,但不幸的是,这不是我想要的。 – Aleksey

+0

所以也许改善你的问题,让我们知道你想要什么:) – Matho

+0

我已经添加了示例图形。 – Aleksey