Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Remove Space between 2 Pushbuttons
Forum Update on Monday, May 27th 2025

Remove Space between 2 Pushbuttons

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 4 Posters 4.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ArunBm
    wrote on last edited by
    #1

    I want to show 10 PushButtons one below the other, without any space just touching each other How do I do this?

    JonBJ 1 Reply Last reply
    0
    • A ArunBm

      I want to show 10 PushButtons one below the other, without any space just touching each other How do I do this?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ArunBm
      Use a QVBoxLayout, and presumably call setSpacing(0), https://doc.qt.io/qt-5/qboxlayout.html#setSpacing.

      1 Reply Last reply
      3
      • A Offline
        A Offline
        ArunBm
        wrote on last edited by
        #3

        @Denni-0 Excellant !!! Thanks

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Here is a version using less layouts for the same effect:

          import sys
          
          from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QVBoxLayout, QPushButton
          
          class MyWindow(QDialog):
              def __init__(self):
                  QDialog.__init__(self)
          
                  self.setWindowTitle('Togetherness')
                  self.resize(200, 400)
          
                  vertical_layout = QVBoxLayout()
                  vertical_layout.setSpacing(0)  # No Spacing
                  vertical_layout.setContentsMargins(0, 0, 0, 0)
          
                  for index in range(10):
                      button = QPushButton(f"Push {index}")
                      vertical_layout.addWidget(button)
          
                  vertical_layout.addStretch(1)  # No Expanding when the window expands
          
                  horizontal_layout = QHBoxLayout(self)
                  horizontal_layout.addLayout(vertical_layout)
                  horizontal_layout.addStretch(1)
          
          if __name__ == '__main__':
              app = QApplication(sys.argv)
          
              MainApp = MyWindow()
              MainApp.show()
          
              sys.exit(app.exec_())
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          2
          • SGaistS SGaist

            Here is a version using less layouts for the same effect:

            import sys
            
            from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QVBoxLayout, QPushButton
            
            class MyWindow(QDialog):
                def __init__(self):
                    QDialog.__init__(self)
            
                    self.setWindowTitle('Togetherness')
                    self.resize(200, 400)
            
                    vertical_layout = QVBoxLayout()
                    vertical_layout.setSpacing(0)  # No Spacing
                    vertical_layout.setContentsMargins(0, 0, 0, 0)
            
                    for index in range(10):
                        button = QPushButton(f"Push {index}")
                        vertical_layout.addWidget(button)
            
                    vertical_layout.addStretch(1)  # No Expanding when the window expands
            
                    horizontal_layout = QHBoxLayout(self)
                    horizontal_layout.addLayout(vertical_layout)
                    horizontal_layout.addStretch(1)
            
            if __name__ == '__main__':
                app = QApplication(sys.argv)
            
                MainApp = MyWindow()
                MainApp.show()
            
                sys.exit(app.exec_())
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @SGaist
            Nice & simple, uses just QVboxLayout + setSpacing() (+ setContentsMargins()) as suggested :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Denni-0 I fail to see why QPushButton should be subclassed.

              As for the fish, I am all for people learning to do things by themselves. However using one layout per button is not the right technique to learn in the use case of the OP.

              The right tool for the right fish.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Denni-0 said in Remove Space between 2 Pushbuttons:

                class MyBttn(QPushButton):
                def init(self, parent, Id, BtnName):
                QPushButton.init(self)
                self.Parent = parent
                self.Id = Id

                    self.btnPush = QPushButton(BtnName)
                    self.clicked.connect(self.MeBenClicked)
                
                    HBox = QHBoxLayout()
                    HBox.addWidget(self.btnPush)
                    HBox.addStretch(1)
                
                    self.setLayout(VBox)
                
                def MeBenClicked(self):
                    self.Parent.BtnClicked(self.Id)
                

                Why are you subleasing QPushButton and creating a QPushButton within it ?
                Your MeBenClicked button violates encapsulation. If you really want to transmit that Id value, create a custom signal in your subclass and emit it when the button clicked. Connect that signal in your MyWindow class. It's not the role of the button to manage what its parent does with the signal.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Tight coupling is not K.I.S.S.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved