Skip to content

Fxcore

Wrapper around the QtCore module for fxgui.

This module provides core functionality and custom classes to enhance the use of QtCore within the fxgui framework.

Classes:

Name Description
FXSortFilterProxyModel

A filter model using fuzzy matching based on SequenceMatcher similarity ratios.

Examples:

Using FXSortFilterProxyModel with a search bar:

>>> from fxgui.fxcore import FXSortFilterProxyModel
>>> proxy = FXSortFilterProxyModel(ratio=0.6)
>>> proxy.setSourceModel(my_model)
>>> search_bar.textChanged.connect(proxy.set_filter_text)

FXSortFilterProxyModel

Bases: QSortFilterProxyModel

A filter model that uses SequenceMatcher to filter items based on a similarity ratio. The similarity ratio is a value between 0 and 1, where 1 indicates a perfect match.

Examples:

Filter a list of items using the FXSortFilterProxyModel

>>> items = ["apple", "banana", "cherry", "date"]
>>> search_bar = QLineEdit()
>>> view = QListView()
>>> model = QStringListModel()
>>> model.setStringList(items)
>>> proxy = FXSortFilterProxyModel()
>>> proxy.setSourceModel(model)
>>> view.setModel(proxy)
>>> search_bar.textChanged.connect(proxy.set_filter_text)
Notes

Base code from Alex Telford: LinkedIn post

Methods:

Name Description
__init__

Initialize the FXSortFilterProxyModel.

data

Get the data for a given role and index.

filterAcceptsRow

Determine whether a row should be accepted by the filter.

lessThan

Compare two indices to determine their order.

set_color_match

Set whether to enable color matching.

set_filter_text

Set the filter text.

set_ratio

Set the ratio threshold for filtering.

set_show_all

Set whether to show all items regardless of the filter.

__init__

__init__(
    ratio: float = 0.5,
    color_match: bool = True,
    parent: Optional[QWidget] = None,
)

Initialize the FXSortFilterProxyModel.

Parameters:

Name Type Description Default

ratio

float

The ratio threshold for filtering.

0.5

color_match

bool

Whether to enable color matching.

True

parent

Optional[QWidget]

The parent widget.

None

data

data(index: QModelIndex, role: int = DisplayRole) -> Optional[QBrush]

Get the data for a given role and index.

Parameters:

Name Type Description Default

index

QModelIndex

The model index.

required

role

int

The role for which data is requested.

DisplayRole

Returns:

Type Description
Optional[QBrush]

The data for the given role and index.

filterAcceptsRow

filterAcceptsRow(source_row: int, source_parent: QModelIndex) -> bool

Determine whether a row should be accepted by the filter.

Parameters:

Name Type Description Default

source_row

int

The source row index.

required

source_parent

QModelIndex

The source parent index.

required

Returns:

Name Type Description
bool bool

True if the row is accepted, False otherwise.

lessThan

lessThan(left: QModelIndex, right: QModelIndex) -> bool

Compare two indices to determine their order.

Parameters:

Name Type Description Default

left

QModelIndex

The left index.

required

right

QModelIndex

The right index.

required

Returns:

Type Description
bool

True if the left index is less than the right index,

bool

False otherwise.

set_color_match

set_color_match(color_match: bool) -> None

Set whether to enable color matching.

Parameters:

Name Type Description Default

color_match

bool

Whether to enable color matching.

required

set_filter_text

set_filter_text(text: str) -> None

Set the filter text.

Parameters:

Name Type Description Default

text

str

The filter text.

required

set_ratio

set_ratio(ratio: float) -> None

Set the ratio threshold for filtering.

Parameters:

Name Type Description Default

ratio

float

The ratio threshold.

required

set_show_all

set_show_all(show_all: bool) -> None

Set whether to show all items regardless of the filter.

Parameters:

Name Type Description Default

show_all

bool

Whether to show all items.

required