Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Why setHorizontalSpacing() is not working?



  • I have a QT Application that uses a GridLayout. I want to decrease the spacing between the widgets but it doesn't. If I set glay1.setHorizontalSpacing() to 60 then it increases the space, but if I want to decrease it then it's not working :(

    Here is the code below:

        right = QtWidgets.QWidget(label)
        right.setStyleSheet("""border: none;""")
        right.setContentsMargins(16, 16, 0, 0)
    
        glay1 = QtWidgets.QGridLayout(right)
        glay1.setSpacing(18)
        glay1.addWidget(vtext, 0, 0)         # the SERVICE text
        glay1.addWidget(vtext1, 1, 0)        # the home icon 
        glay1.setHorizontalSpacing(0)
        glay1.addWidget(butt1, 1, 1)         # the Home text
    

    The following code will give the output:

    Capture.PNG

    I want to decrease the space between the home icon and button. What I am doing wrong here?



  • @Abhay-Salvi
    Further to my learned colleague @jsulm 's remark. Have a look at overload void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment()), https://doc.qt.io/qt-5/qgridlayout.html#addWidget-2, which would allow you to span the SERVICES label across two columns so that the HOME text can move closer to its label.

    A separate matter is whether you need a grid layout here for what you are going to do with further items. A list of items with icons, or even a menu with icons, might be more appropriate.


  • Qt Champions 2019

    @Abhay-Salvi This is because of the SERVICE label which takes some space, so first column has a minimum width of SERVICES label.



  • @Abhay-Salvi
    Further to my learned colleague @jsulm 's remark. Have a look at overload void QGridLayout::addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment()), https://doc.qt.io/qt-5/qgridlayout.html#addWidget-2, which would allow you to span the SERVICES label across two columns so that the HOME text can move closer to its label.

    A separate matter is whether you need a grid layout here for what you are going to do with further items. A list of items with icons, or even a menu with icons, might be more appropriate.


  • Banned

    Here is a MUC demonstrating a few things using that image above -- I will first state it is not a clean representation due to the various elements being demonstrated but you should be able to take the concepts presented here and fix the issue you are having and perhaps take it a step further should you care too. I would also mirror JonB's question about whether or not you should be using a Grid Layout as opposed to just the normal layout functionality

    from PyQt5.QtCore    import pyqtSlot
    from PyQt5.QtGui     import QFont
    from PyQt5.QtWidgets import QApplication, QStyleFactory, QMainWindow, QWidget
    from PyQt5.QtWidgets import QGridLayout, QHBoxLayout, QVBoxLayout
    from PyQt5.QtWidgets import QLabel, QTextEdit, QPushButton
    
    class CentralPanel(QWidget):
        def __init__(self, parent):
            QWidget.__init__(self)
            self.Parent = parent
    
            HdrFont = QFont()
            HdrFont.setPointSize(14)
            HdrFont.setBold(True)
    
            lblHedr = QLabel('Services')
            lblHedr.setFont(HdrFont)
    
            Font = QFont()
            Font.setBold(True)
    
          # Just using a Button to mimic the Icon that I do not have
            btnIcon = QPushButton('Hm')
            btnIcon.setFont(Font)
            BtnWdth=30
            btnIcon.setFixedWidth(BtnWdth)
    
          # The preceding blank is just a demo of how to add spacing here
            lblText = QLabel('Home')
            
          # Placing these two together within a left aligned Box
            HBox1 = QHBoxLayout()
            HBox1.addWidget(btnIcon)
            HBox1.addWidget(lblText)
            HBox1.addStretch(1)
    
            self.txeLeftStuff = QTextEdit()
    
            grdLftSde = QGridLayout()
            grdLftSde.addWidget(lblHedr, 0, 0)
            grdLftSde.addLayout(HBox1, 1, 0)
            grdLftSde.addWidget(self.txeLeftStuff, 2, 0)
    
            self.txeRiteStuff = QTextEdit()
    
            BtnWdth=80; BtnHght=40
            self.btnNext = QPushButton("Next")
            self.btnNext.setFont(Font)
            self.btnNext.setFixedSize(BtnWdth, BtnHght)
            self.btnNext.clicked.connect(self.GotoNext)
    
            HBox2 = QHBoxLayout()
            HBox2.addStretch(1)
            HBox2.addWidget(self.btnNext)
    
          # Add the display to the general layout
            vbxRteSde = QVBoxLayout()
            vbxRteSde.addWidget(self.txeRiteStuff)
            vbxRteSde.addLayout(HBox2)
            
            HBox3 = QHBoxLayout()
            HBox3.addLayout(grdLftSde)
            HBox3.addLayout(vbxRteSde)
    
            self.setLayout(HBox3)
    
        @pyqtSlot()
        def GotoNext(self):
            self.Parent.SetStatusBar("Moving to Next Page")
    
    class TestApp(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            self.setWindowTitle('Test Application')
            WinLeft = 550; WinTop = 350; WinWidth = 450; WinHigh = 350
            self.setGeometry(WinLeft, WinTop, WinWidth, WinHigh)
    
            self.CenterPane = CentralPanel(self)
            self.setCentralWidget(self.CenterPane)
    
            self.StatBar = self.statusBar()
            self.SetStatusBar('Ready')
    
            self.setStyle(QStyleFactory.create('Cleanlooks'))
    
        def SetStatusBar(self, StatusMsg):
            if len(StatusMsg) > 0:
                self.StatBar.showMessage(StatusMsg)
    
    if __name__ == "__main__":
        MainEventHandler = QApplication([])
    
        MainApplication = TestApp()
        MainApplication.show()
        
        MainEventHandler.exec()
    


  • @Denni-0 Thanks for your answer :) I got it


Log in to reply