2016-09-29 1103 views
-1

我想创建一个包含2个小部件的子容器布局。这两个小工具应该放在一起,但我目前的设置仍然有一定的间距。如何去除gridLayout(QT)中的间距?

我已经将间距设置为0 setSpacing(0)。而setContentsMargins(0,0,0,0)没有帮助。

我正在使用PyQt5,但它不应该是一个转换C++代码的问题。

正如你可以在图片中看到仍然有小的差距:

(左:LineEdit - 右:可通过按钮)

import PyQt5.QtCore as qc 
import PyQt5.QtGui as qg 
import PyQt5.QtWidgets as qw 

import sys 

class Window(qw.QWidget): 
    def __init__(self): 
     qw.QWidget.__init__(self) 

     self.initUI() 

    def initUI(self): 
     gridLayout = qw.QGridLayout() 

     height = 20 

     self.label1 = qw.QLabel("Input:") 
     self.label1.setFixedHeight(height) 
     gridLayout.addWidget(self.label1, 0, 0) 

     # Child Container 
     childGridLayout = qw.QGridLayout() 
     childGridLayout.setContentsMargins(0,0,0,0) 
     childGridLayout.setHorizontalSpacing(0) 

     self.lineEdit1 = qw.QLineEdit() 
     self.lineEdit1.setFixedSize(25, height) 
     childGridLayout.addWidget(self.lineEdit1, 0, 0) 

     self.pushButton1 = qw.QPushButton("T") 
     self.pushButton1.setFixedSize(20, height) 
     childGridLayout.addWidget(self.pushButton1, 0, 1) 
     # ----------------- 
     gridLayout.addItem(childGridLayout, 0,1) 

     self.setLayout(gridLayout) 


if __name__ == '__main__': 

    app = qw.QApplication(sys.argv) 
    window = Window() 
    window.show() 
    sys.exit(app.exec_()) 
+0

你应该张贴您的代码,并提供了一个最小的可核查的例子:http://stackoverflow.com/help/mcve –

+0

好感谢,我加入了一个示例代码。 – Sens4

回答

0

Qt文档说: 默认情况下, QLayout使用由样式提供的值。在大多数平台上,所有方向的边距都是11像素。

编号:http://doc.qt.io/qt-4.8/qlayout.html#setContentsMargins

所以你可能需要使用 “setHorizo​​ntalSpacing(INT间隔)” 的水平空间和 “setVerticalSpacing(INT间隔)” 垂直。

根据文档,这可能会删除您的案例中的空间。 编号:http://doc.qt.io/qt-4.8/qgridlayout.html#horizontalSpacing-prop

如果没有解决,还有就是覆盖样式设置为空间(从哪里得到的布局)选项....我想如果要提供自定义布局间距这是乏味

在一个QStyle子类中,在你的子类中实现一个名为layoutSpacingImplementation()的槽。

更多detials: http://doc.qt.io/qt-4.8/qstyle.html#layoutSpacingImplementation