controller.utils.keyword_highlight_delegate
- File:
EuljiroWorship/controller/utils/keyword_highlight_delegate.py
- Author:
Benjamin Jaedon Choi - https://github.com/saintbenjamin
- Affiliated Church:
The Eulji-ro Presbyterian Church [대한예수교장로회(통합) 을지로교회]
- Address:
The Eulji-ro Presbyterian Church, 24-10, Eulji-ro 20-gil, Jung-gu, Seoul 04549, South Korea
- Telephone:
+82-2-2266-3070
- E-mail:
euljirochurch [at] G.M.A.I.L. (replace [at] with @ and G.M.A.I.L as you understood.)
- License:
MIT License with Attribution Requirement (see LICENSE file for details); Copyright (c) 2025 The Eulji-ro Presbyterian Church.
QStyledItemDelegate implementation for keyword highlighting in Qt item views.
This module defines a delegate that renders cell text using QTextDocument so that:
HTML tags (e.g.,
<b>) are supported.Newlines are rendered as multi-line content (
<br>).Specified keywords are highlighted by wrapping them in red
<span>tags.Row height can be computed from the HTML-rendered content.
Typical usage:
view.setItemDelegate(KeywordHighlightDelegate(keywords=["하나님", "세상"]))
- class controller.utils.keyword_highlight_delegate.KeywordHighlightDelegate(*args, **kwargs)[source]
Bases:
QStyledItemDelegateCustom item delegate that highlights keywords in model text using HTML rendering.
This delegate renders each cell via
QTextDocumentso it can support:HTML formatting (keyword highlighting)
Multi-line wrapping
Accurate
sizeHint()based on rendered document height
- keywords
Keywords to highlight. Empty strings are ignored. Matching is performed via simple substring replacement after HTML-escaping the cell text.
- Type:
list[str]
- __init__(keywords, parent=None)[source]
Initialize the delegate with keywords to highlight.
- Parameters:
keywords (list[str]) – Keywords to highlight. Empty strings are ignored.
parent (QWidget | None) – Optional parent widget.
- Returns:
None
- paint(painter, option, index)[source]
Paint a single cell using HTML rendering, highlighting configured keywords.
Behavior:
Fills the background depending on selection state.
Converts the cell text to HTML with keyword highlighting.
Uses QTextDocument to render the HTML with wrapping.
Draws the document contents with a small padding offset.
- Parameters:
painter (QPainter) – Painter used for drawing.
option (QStyleOptionViewItem) – Style and geometry information for the item.
index (QModelIndex) – Model index for the cell being painted.
- Returns:
None
- _highlight_keywords(text)[source]
Convert raw text into HTML with keyword highlighting.
This method:
Escapes HTML special characters (
&,<,>).Converts newline characters to
<br>.Wraps each keyword occurrence with:
<span style='color:red'>...</span>
Note
This implementation performs simple string replacement and does not use regex or word-boundary matching.
- Parameters:
text (str) – Original cell text.
- Returns:
HTML-formatted string with highlighted keywords.
- Return type:
str
- sizeHint(option, index)[source]
Return the preferred size for a cell based on HTML-rendered content.
Computes the document size using QTextDocument with the same width constraints used during painting, then adds padding so the content does not touch borders.
- Parameters:
option (QStyleOptionViewItem) – Style and geometry information for the item.
index (QModelIndex) – Model index for the cell.
- Returns:
Preferred size for the rendered cell content.
- Return type:
QSize