1

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.

I have already set the spacing to 0 setSpacing(0). And setContentsMargins(0,0,0,0) doesn't helped.

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

As you can see in the picture there is still a small gap:

(Left: LineEdit - Right: PushButton)

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_())
Sens4
  • 605
  • 1
  • 11
  • 29

1 Answers1

2

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

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

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

Based on the documentation, this may delete space in your case. Ref:http://doc.qt.io/qt-4.8/qgridlayout.html#horizontalSpacing-prop

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

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

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

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34