Skip to content

Instantly share code, notes, and snippets.

@MaurizioB
Created July 17, 2021 16:22
Show Gist options
  • Save MaurizioB/faedf525b4beb8910d3d25ebff66b984 to your computer and use it in GitHub Desktop.
Save MaurizioB/faedf525b4beb8910d3d25ebff66b984 to your computer and use it in GitHub Desktop.
QPushButton with embedded QLabel
from PyQt5 import QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
mainLayout = QtWidgets.QGridLayout(window)
labels = []
for r in range(3):
for c in range(4):
button = QtWidgets.QPushButton()
label = QtWidgets.QLabel()
# possible workaround:
# button.sizeHint = label.sizeHint
mainLayout.addWidget(button, r, c)
button.setStyleSheet('QPushButton {border: none;}')
buttonLayout = QtWidgets.QVBoxLayout(button)
buttonLayout.setContentsMargins(0, 0, 0, 0)
labels.append(label)
buttonLayout.addWidget(label)
le = QtWidgets.QLineEdit(placeholderText='button text')
mainLayout.addWidget(le, mainLayout.rowCount(), 0, 1, 4)
le.textChanged.connect(lambda text: [l.setText(text) for l in labels])
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment