- 1 year ago
Remove the strange one-pixel space around QPushButton with PySide/PyQT on Windows
To remove the one-pixel space around a QPushButton
in PySide/PyQt on Windows, you can modify the style sheet of the button to adjust its padding and border properties. Here's an example:
pythonimport sys
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
class MyWidget(QWidget):
def __init__(self):
super().__init__()
button = QPushButton("Button", self)
button.setStyleSheet("""
QPushButton {
padding: 0px;
border: none;
}
""")
self.setGeometry(100, 100, 200, 100)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
In this example, we create a QPushButton
and set its style sheet using setStyleSheet()
. Within the style sheet, we adjust the padding
to 0px
and set the border
to none
. This removes any padding and border around the button, effectively eliminating the one-pixel space.
You can modify the style sheet further to achieve the desired appearance. Experiment with different values for padding
, border
, and other properties to suit your requirements.
Keep in mind that adjusting the style sheet affects the visual appearance of the button, and the solution provided here is specific to PySide/PyQt on Windows. It may not produce the same results on other platforms or with different styles.