How to remove spacing inside a gridLayout (QT)?
<p>I want to create a child container layout which will contains 2 widgets. Those 2 widgets should be placed right next to each other but my current setup still has some spacing in between.</p>

<p>I have already set the spacing to 0 <code>setSpacing(0)</code>. And <code>setContentsMargins(0,0,0,0)</code> doesn't helped.</p>

<p>I am using PyQt5 but it shouldn't be a problem converting c++ code.</p>

<p>As you can see in the picture there is still a small gap:</p>

<p><a href="http://i.stack.imgur.com/PiuHk.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/PiuHk.jpg" alt=""></a>
(Left: LineEdit - Right: PushButton)</p>

<pre><code>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_())
</code></pre>

<p>The QT documentation says:
<strong>By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.</strong></p>

<p>Ref:<a href="http://doc.qt.io/qt-4.8/qlayout.html#setContentsMargins" rel="nofollow">http://doc.qt.io/qt-4.8/qlayout.html#setContentsMargins</a></p>

<p>So you may need to use "setHorizontalSpacing(int spacing)"  for horizontal space and "setVerticalSpacing(int spacing)" for vertical.</p>

<p>Based on the documentation, this may delete space in your case.
Ref:<a href="http://doc.qt.io/qt-4.8/qgridlayout.html#horizontalSpacing-prop" rel="nofollow">http://doc.qt.io/qt-4.8/qgridlayout.html#horizontalSpacing-prop</a></p>

<p>If not resolved, there is an option to override style settings for space (from where the layout gets).... I think this is tedious</p>

<p>If you want to provide custom layout spacings in a QStyle subclass, implement a slot called layoutSpacingImplementation() in your subclass.</p>

<p>More detials:
<a href="http://doc.qt.io/qt-4.8/qstyle.html#layoutSpacingImplementation" rel="nofollow">http://doc.qt.io/qt-4.8/qstyle.html#layoutSpacingImplementation</a></p>

python c++ qt pyqt qgridlayout