Skip to content

Instantly share code, notes, and snippets.

@ShenTengTu
Created March 26, 2024 05:13
Show Gist options
  • Save ShenTengTu/78b28f8447221d041ed2972e18aa8a3e to your computer and use it in GitHub Desktop.
Save ShenTengTu/78b28f8447221d041ed2972e18aa8a3e to your computer and use it in GitHub Desktop.
Function that generates a stylesheet for styling QLineEdit.
import sys
from qtpy.QtWidgets import (
QApplication,
QMainWindow,
QLineEdit,
QFrame,
QGridLayout,
)
def style_QLineEdit(
border_width: str = "1px",
border_radius: str = "4px",
horizontal_padding: str = "4px",
vertical_padding: str = "2px",
border_color: str = "#b0b0b0",
background_color: str = "#fdfdfd",
color: str = "#404040",
placeholder_color: str = "#808080",
selection_background_color: str = "#404040",
selection_color: str = "#fdfdfd",
hover_border_color: str = "#404040",
disabled_border_color: str = "#b0b0b0",
disabled_background_color: str = "#e0e0e0",
disabled_color: str = "#808080",
password_character: str = chr(9679),
password_mask_delay: int = 0,
):
qss = f"""
QLineEdit {{
border-width: {border_width};
border-style: solid;
border-color: {border_color};
border-radius: {border_radius};
background-color: {background_color};
color: {color};
padding: {vertical_padding} {horizontal_padding};
placeholder-text-color: {placeholder_color};
selection-background-color: {selection_background_color};
selection-color: {selection_color};
lineedit-password-character: {ord(password_character)};
lineedit-password-mask-delay: {password_mask_delay};
}}
QLineEdit::hover {{
border-color: {hover_border_color};
}}
QLineEdit::disabled {{
border-color: {disabled_border_color};
background-color: {disabled_background_color};
color: {disabled_color};
}}
"""
return qss
class Frame(QFrame):
def __init__(self, parent=None):
super().__init__(parent)
layout = QGridLayout()
layout.setSizeConstraint(QGridLayout.SizeConstraint.SetFixedSize)
w = QLineEdit()
w.setPlaceholderText("Placeholder")
layout.addWidget(w, 0, 0)
w = QLineEdit()
w.setPlaceholderText("Placeholder")
w.setDisabled(True)
layout.addWidget(w, 0, 1)
w = QLineEdit()
w.setEchoMode(QLineEdit.EchoMode.Password)
w.setPlaceholderText("Password")
layout.addWidget(w, 1, 0)
self.setLayout(layout)
def main():
app = QApplication(sys.argv)
app.setStyleSheet(style_QLineEdit())
w = QMainWindow()
w.setMinimumSize(200, 100)
w.setCentralWidget(Frame(w))
w.show()
sys.exit(app.exec())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment