From a17d022463fc0e0a28c888cedf018c3c964ef110 Mon Sep 17 00:00:00 2001 From: ConfuSomu Date: Sat, 21 Jan 2023 19:35:32 -0500 Subject: Fix out of range errors in on_buttonSearch_clicked --- src/mainwindow.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b33889a..28af716 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,6 +8,7 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -103,13 +104,15 @@ void MainWindow::on_buttonSearch_clicked() { QString current_search = ui->textInputSearch->text(); + QList matches = ui->listWidget->findItems(current_search, Qt::MatchContains); + if (not (*last_search == current_search)) { *last_search = current_search; selected_match = 0; - } else - ++selected_match; + } else if (++selected_match >= matches.size()) selected_match = 0; + + qDebug() << matches.size() << selected_match; - QList matches = ui->listWidget->findItems(current_search, Qt::MatchContains); if (not matches.isEmpty()) { ui->listWidget->scrollToItem(matches[selected_match], QAbstractItemView::EnsureVisible); ui->listWidget->setCurrentItem(matches[selected_match]); -- cgit v1.2.3-54-g00ecf