How to reset the column span of a widget in a QGridLayout?

3k views Asked by At

Is possible to set the column span of a QLineEdit box after it has been added to the layout? I have two QLineEdit boxes in a QGridLayout that are right next to each other horizontally. During the execution of some of my code, one of these boxes gets hidden. I would like to increase the column span where the hidden one was to avoid a weird gap at the end, and reduce it when needed.

I couldn't really find anything in the Qt documentation for this type of change beyond making the adjustment prior to adding the widget to the layout.

2

There are 2 answers

2
ekhumoro On BEST ANSWER

There no method for resetting the row- or column-span after a widget has been added. However, addWidget can be called again on the same widget to achieve the same affect, because re-adding a widget to the same layout always implicitly removes it first. So something like this should work:

index = layout.indexOf(widget)
row, column = layout.getItemPosition(index)[:2]
layout.addWidget(widget, row, column, rowspan, colspan)

Here is a simple demo script:

import sys
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.button = QtWidgets.QPushButton('Toggle Edit')
        self.button.clicked.connect(self.handleButton)
        self.edit1 = QtWidgets.QLineEdit()
        self.edit1.setPlaceholderText('One')
        self.edit2 = QtWidgets.QLineEdit()
        self.edit2.setPlaceholderText('Two')
        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.edit1, 0, 0)
        layout.addWidget(self.edit2, 0, 1)
        layout.addWidget(self.button, 1, 0)

    def handleButton(self):
        if self.edit2.isHidden():
            self.setWidgetSpan(self.edit1, 1, 1)
            self.edit2.setHidden(False)
        else:
            self.edit2.setHidden(True)
            self.setWidgetSpan(self.edit1, 1, 2)

    def setWidgetSpan(self, widget, rowspan=1, colspan=1):
        layout = self.layout()
        index = layout.indexOf(widget)
        row, column = layout.getItemPosition(index)[:2]
        layout.addWidget(widget, row, column, rowspan, colspan)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 300, 100)
    window.show()
    sys.exit(app.exec_())
0
Pavan Chandaka On

Try as said below.

When you add two QLineEdit, The QGridLayout dynamically creates two columns.

If you want extra columns for spanning use QSpacerItem and get extra columns. And you have to use addItem(..,..,..) of QGridLayout to add the spacer item to layout.

Between your two QLineEdit add spacers as shown below

spacer =  QtGui.QSpacerItem(20, 20)
layout.addItem(spacer,0,1)

Now if you add totak 4 spacers (for example) ,

spacer1 =  QtGui.QSpacerItem(20, 20)
layout.addItem(spacer1,0,2)

spacer2 =  QtGui.QSpacerItem(20, 20)
layout.addItem(spacer2,0,3)

spacer3 =  QtGui.QSpacerItem(20, 20)
layout.addItem(spacer3,0,4)

Now you have total 6 columns ------- with first QLineEdit (column 0), 4 spacers, last QLineEdit (column 5)

Now you can use setColumnStretch(column,stretch) to set the span of any line edit. Here I am trying to set span for last QLineEdit..as shown below. Spanned for 3 columns...

layout.setColumnStretch(5,3) 

Hope this helps...