Solved Remove Space between 2 Pushbuttons
-
I want to show 10 PushButtons one below the other, without any space just touching each other How do I do this?
-
@ArunBm
Use aQVBoxLayout
, and presumably callsetSpacing(0)
, https://doc.qt.io/qt-5/qboxlayout.html#setSpacing. -
@Denni-0 Excellant !!! Thanks
-
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_())
-
@SGaist
Nice & simple, uses justQVboxLayout
+setSpacing()
(+setContentsMargins()
) as suggested :) -
@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.
-
@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 = Idself.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 ?
YourMeBenClicked
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. -
Tight coupling is not K.I.S.S.