Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/qt/addressbookpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel

auto address = model->index(row, AddressTableModel::Address, parent);

if (filterRegExp().indexIn(model->data(address).toString()) < 0 &&
filterRegExp().indexIn(model->data(label).toString()) < 0) {
return false;
}

return true;
const auto pattern = filterRegExp();
return (model->data(address).toString().contains(pattern) ||
model->data(label).toString().contains(pattern));
}
};

Expand Down Expand Up @@ -143,7 +140,9 @@ void AddressBookPage::setModel(AddressTableModel *_model)
proxyModel = new AddressBookSortFilterProxyModel(type, this);
proxyModel->setSourceModel(_model);

connect(ui->searchLineEdit, &QLineEdit::textChanged, proxyModel, &QSortFilterProxyModel::setFilterWildcard);
connect(ui->searchLineEdit, &QLineEdit::textChanged, [this](const QString& pattern) {
proxyModel->setFilterFixedString(pattern);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact setFilterWildcard in Qt6 already handles the change noted in the porting documentation:

There is no direct way to do wildcard matching in QRegularExpression. However, the wildcardToRegularExpression method is provided to translate glob patterns into a Perl-compatible regular expression that can be used for that purpose.

Qt5

void QSortFilterProxyModel::setFilterFixedString(const QString &pattern)
{
    Q_D(QSortFilterProxyModel);
    d->filter_about_to_be_changed();
    QRegExp rx(pattern, d->filter_data.caseSensitivity(), QRegExp::FixedString);
    d->filter_data.setRegExp(rx);
    d->filter_changed();
}

Qt6

void QSortFilterProxyModel::setFilterWildcard(const QString &pattern)
{
    Q_D(QSortFilterProxyModel);
    d->filter_regularexpression.removeBindingUnlessInWrapper();
    d->filter_about_to_be_changed();
    d->set_filter_pattern(QRegularExpression::wildcardToRegularExpression(
            pattern, QRegularExpression::UnanchoredWildcardConversion));
    d->filter_changed(QSortFilterProxyModelPrivate::Direction::Rows);
    d->filter_regularexpression.notify();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

ui->tableView->setModel(proxyModel);
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
Expand Down
52 changes: 49 additions & 3 deletions src/qt/test/addressbooktests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <chrono>

#include <QApplication>
#include <QLineEdit>
#include <QMessageBox>
#include <QTableView>
#include <QTimer>
Expand Down Expand Up @@ -102,11 +103,13 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
QString s_label("already here (s)");

// Define a new address (which should add to the address book successfully).
QString new_address;
QString new_address_a;
QString new_address_b;

std::tie(r_key_dest, preexisting_r_address) = build_address();
std::tie(s_key_dest, preexisting_s_address) = build_address();
std::tie(std::ignore, new_address) = build_address();
std::tie(std::ignore, new_address_a) = build_address();
std::tie(std::ignore, new_address_b) = build_address();

{
LOCK(wallet->cs_wallet);
Expand Down Expand Up @@ -159,9 +162,52 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
// Submit a new address which should add successfully - we expect the
// warning message to be blank.
EditAddressAndSubmit(
&editAddressDialog, QString("new"), new_address, QString(""));
&editAddressDialog, QString("io - new A"), new_address_a, QString(""));
check_addbook_size(3);
QCOMPARE(table_view->model()->rowCount(), 2);

EditAddressAndSubmit(
&editAddressDialog, QString("io - new B"), new_address_b, QString(""));
check_addbook_size(4);
QCOMPARE(table_view->model()->rowCount(), 3);

auto search_line = address_book.findChild<QLineEdit*>("searchLineEdit");

search_line->setText(r_label);
QCOMPARE(table_view->model()->rowCount(), 0);

search_line->setText(s_label);
QCOMPARE(table_view->model()->rowCount(), 1);

search_line->setText("io");
QCOMPARE(table_view->model()->rowCount(), 2);

// Check wilcard "?".
search_line->setText("io?new");
QCOMPARE(table_view->model()->rowCount(), 0);
search_line->setText("io???new");
QCOMPARE(table_view->model()->rowCount(), 0);

// Check wilcard "*".
search_line->setText("io*new");
QCOMPARE(table_view->model()->rowCount(), 0);
search_line->setText("*");
QCOMPARE(table_view->model()->rowCount(), 0);

search_line->setText(preexisting_r_address);
QCOMPARE(table_view->model()->rowCount(), 0);

search_line->setText(preexisting_s_address);
QCOMPARE(table_view->model()->rowCount(), 1);

search_line->setText(new_address_a);
QCOMPARE(table_view->model()->rowCount(), 1);

search_line->setText(new_address_b);
QCOMPARE(table_view->model()->rowCount(), 1);

search_line->setText("");
QCOMPARE(table_view->model()->rowCount(), 3);
}

} // namespace
Expand Down