Back to C++
2026-03-149 min read

Fullscreen Search (C++)

Learn Fullscreen Search (C++) step by step with clear examples and exercises.

Why This Matters

In a graphical user interface (GUI), providing a fullscreen search bar can significantly enhance user experience by allowing users to quickly find and access content within the application. This feature is essential for applications with large amounts of data or complex interfaces, as it helps users save time and effort in navigating through various sections.

In this lesson, we will learn how to create a fullscreen search bar in C++ using the Qt framework, which is a popular cross-platform GUI library. This knowledge can be beneficial for creating efficient and user-friendly applications, making you more competitive in the job market as well as helping you tackle real-world programming challenges.

Prerequisites

To follow this lesson, you should have a basic understanding of:

  1. C++ programming language syntax and concepts
  2. Object-oriented programming principles
  3. Qt framework basics (Qt Creator, QMainWindow, QLineEdit, QPushButton)
  4. Familiarity with Qt's event loop and signal/slot mechanism
  5. Basic understanding of Qt's UI design and layout system

Core Concept

In this section, we will create a simple fullscreen search application using the Qt framework in C++. Our application will consist of:

  1. A main window with a menu bar
  2. A central widget containing a search field and a list of items to be searched
  3. A search function that filters the list based on user input
  4. Keyboard shortcuts for navigating through the list and searching
  5. Customization options such as sorting and filtering options
  6. Error handling and validation for user input

Setting up the project

First, make sure you have Qt installed on your system. You can download it from here. Follow the instructions to install Qt Creator, which is an integrated development environment (IDE) for developing applications using the Qt framework.

Next, create a new project by selecting File > New File or Project and then choosing Qt > Application. Select the "Widget" application template and click Next. Name your project "FullscreenSearch", choose a location to save it, and click Finish.

Designing the user interface

Now that we have our project set up, let's design the user interface. In Qt Creator, open the mainwindow.ui file located in the resources/ui directory of your project. This file contains a visual representation of the main window and its components.

By default, you will see a QMainWindow with a menu bar and a central widget. Add a QLineEdit (search field), a QPushButton (search button), and a QListWidget (list of items to be searched) to the central widget. You can do this by selecting the central widget in the mainwindow.ui file, clicking on the "Form" tab, and then dragging and dropping the necessary components from the "Form" toolbox onto the central widget.

Next, set properties for the QLineEdit, QPushButton, and QListWidget:

  1. QLineEdit (search field):
  • Set object name to searchField
  • Set minimum width to 200 pixels
  • Check the "Editable" checkbox
  • Check the "Frame" checkbox and set style to "Plain"
  1. QPushButton (search button):
  • Set object name to searchButton
  • Set text to "Search"
  • Check the "Auto Exclusive" checkbox
  1. QListWidget (list of items to be searched):
  • Set object name to itemList
  • Set minimum width to 400 pixels
  • Check the "View" checkbox and set mode to "List Mode"

Save the mainwindow.ui file after making these changes.

Implementing the search function

Now that we have our user interface set up, let's implement the search function in C++ code. To do this, open the mainwindow.cpp file located in the src directory of your project.

First, include necessary headers:

#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
#include <QListWidget>
#include <QKeySequence>
#include <QStringListModel>
#include <QSortFilterProxyModel>

Next, define a list of items to be searched in the constructor:

QStringList items = {
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
"Item 6", "Item 7", "Item 8", "Item 9", "Item 10"
};

In the constructor, set up the central widget and connect the search button's clicked signal to a slot that filters the list based on user input:

FullscreenSearch::FullscreenSearch(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {

ui->setupUi(this);

// Set up central widget and list of items
QListWidget *listWidget = new QListWidget(this);
for (const auto &item : items) {
listWidget->addItem(item.toUtf8().data());
}
auto model = new QStringListModel(this);
model->setStringList(items);
auto proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);
ui->itemList->setModel(proxyModel);

// Connect search button to filterItems() slot
connect(ui->searchButton, &QPushButton::clicked, this, &FullscreenSearch::filterItems);
}

Now, implement the filterItems() function that filters the list based on user input from the search field:

void FullscreenSearch::filterItems() {
QString searchText = ui->searchField->text();
proxyModel->setFilterFixedString(searchText, Qt::CaseInsensitive);
}

Save the mainwindow.cpp file after making these changes.

Implementing keyboard shortcuts

To make our application more user-friendly, let's implement keyboard shortcuts for navigating through the list and searching. Open the FullscreenSearch.pro file located in the root directory of your project and add the following line to the QT += section:

widgets
shortcut

Next, open the mainwindow.cpp file again and define a shortcut for searching:

void FullscreenSearch::defineShortcuts() {
// Define search shortcut (Ctrl+F)
QShortcut *searchShortcut = new QShortcut(QKeySequence("Ctrl+F"), this);
connect(searchShortcut, &QShortcut::activated, this, &FullscreenSearch::focusSearchField);
}

In the constructor, call the defineShortcuts() function after setting up the central widget:

void FullscreenSearch::FullscreenSearch(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {

// ... (existing code)

defineShortcuts();
}

Now, implement the focusSearchField() function that sets focus to the search field when the search shortcut is activated:

void FullscreenSearch::focusSearchField() {
ui->searchField->setFocus();
}

Save the mainwindow.cpp file after making these changes.

Customizing the application

To provide more functionality, we can add customization options such as sorting and filtering options:

  1. Sorting: Implement a function to sort the list alphabetically when no search text is entered. This can be done using the sort() function of the QStringListModel.
  2. Filtering: Modify the application to allow users to search for items containing specific words instead of exact matches (case-insensitive partial matching). This can be achieved by modifying the filterItems() function to use a regular expression or QRegExp.
  3. Clear button: Add a "Clear" button to clear the search field and show all items in the list. This can be done by setting the filter string of the proxy model to an empty string.
  4. Keyboard navigation: Implement keyboard shortcuts for navigating through the list using the Up and Down arrow keys without activating the search function. This can be achieved by connecting signals from the QListWidget to functions that handle keyboard events.

Error handling and validation

To ensure a smooth user experience, we should also implement error handling and validation for user input:

  1. Input validation: Validate user input in the search field to prevent empty or invalid entries. This can be done using regular expressions or QRegExp to check for specific patterns.
  2. Error messages: Display error messages when invalid input is detected, such as an empty search field or an unsupported filter option.
  3. Exception handling: Handle exceptions that may occur during runtime, such as out-of-memory errors or network connectivity issues.

Worked Example

In this section, we will walk through an example of using our fullscreen search application with customization options.

  1. Start the application by clicking on the "Run" button (green triangle) in Qt Creator.
  2. In the search field, type "Item".
  3. Press Enter or click the Search button to filter the list based on the entered text. You should see all items that contain "Item" in their name.
  4. To sort the list alphabetically, clear the search field and press Ctrl+S (sort shortcut). The list will now be sorted alphabetically.
  5. To search for items containing specific words instead of exact matches, type "it" in the search field and press Enter or click the Search button. You should see all items that contain "it" in any part of their name.
  6. To clear the search field and show all items in the list, click on the "Clear" button (if added).
  7. To navigate through the list using keyboard shortcuts, press the Up and Down arrow keys.
  8. Press Ctrl+F to focus the search field again.
  9. Clear the search field by pressing Backspace or Delete, then press Enter or click the Search button to see all items in the list (if sorted).

Common Mistakes

1. Forgetting to connect the search button's clicked signal to the filterItems() slot

Ensure that you have connected the search button's clicked signal to the filterItems() function as shown in the Core Concept section.

2. Not setting up the central widget correctly

Make sure you set up the central widget and add the list of items to it properly, as demonstrated in the Core Concept section.

3. Implementing search without error handling or validation

Remember to implement input validation, error messages, and exception handling to ensure a smooth user experience.

Practice Questions

  1. Modify the application to allow users to search for items containing specific words instead of exact matches (case-insensitive partial matching).
  2. Implement a function that sorts the list of items alphabetically when no search text is entered.
  3. Add a "Clear" button to clear the search field and show all items in the list.
  4. Implement keyboard shortcuts for navigating through the list using the Up and Down arrow keys without activating the search function.
  5. Implement input validation for the search field to prevent empty or invalid entries.
  6. Display error messages when invalid input is detected, such as an empty search field or an unsupported filter option.
  7. Handle exceptions that may occur during runtime, such as out-of-memory errors or network connectivity issues.

FAQ

Q: How can I add more items to the list?

A: You can modify the items QStringList variable in the constructor of the main window to include additional items as needed.

Q: Why is my search not case-sensitive?

A: By default, the Qt framework's contains() function is case-insensitive when comparing strings. If you need exact matches, you can use Qt::CaseSensitive instead of Qt::CaseInsensitive.

Q: Why are my keyboard shortcuts not working?

A: Make sure you have included the necessary headers and defined the shortcuts properly, as demonstrated in the Core Concept section. Also, ensure that your Qt Creator project configuration includes the "shortcut" module, as described earlier in this lesson.

Q: Why is my application not sorting the list alphabetically when no search text is entered?

A: Make sure you have implemented the sorting function and connected it to a signal or timer that triggers the sorting when the search field is empty.

Q: Why am I seeing error messages when I enter invalid input in the search field?

A: Ensure that you have implemented input validation, error messages, and exception handling as described in the Core Concept section.

Q: Why are my keyboard navigation shortcuts not working for navigating through the list without activating the search function?

A: Make sure you have connected signals from the QListWidget to functions that handle keyboard events, as demonstrated in the Customizing the Application section.

Fullscreen Search (C++) | C++ | XQA Learn