Chapter 11. Selection Elements

Every developer has faced this: the interface seems to work, but users get confused, click “the wrong place”, and make mistakes. There’s choice—but it’s implemented in a way that becomes a source of problems rather than convenience.

This chapter will reveal why selection widgets are one of the trickiest parts of GUI. You’ll discover how simple lists, tables, and tabs can either speed up user work or invisibly destroy UX and application performance.

We’ll show how QListWidget, QTreeWidget, and QTableWidget differ in practice, when icon mode becomes a trap, and why adding widgets inside items can slow down the app significantly. We’ll also cover signals, sorting, drag & drop, and editing nuances—just enough to see the whole system.

Skipping this chapter means later paying for architectural decisions made “by eye”.

This chapter includes ready-to-use code examples.

Chapter Self-Check

Why does using widgets in list items (QListWidget, QTreeWidget) significantly reduce performance?Answer
Correct answer: Each widget requires its own rendering, event processing, and resource management. With hundreds of items, this creates serious overhead, so it’s recommended to use representations (e.g., checkState for checkboxes) instead of actual widgets.
What’s the key advantage of model-view architecture over QListWidget, QTreeWidget, and QTableWidget classes?Answer
Correct answer: Model-view architecture separates data from its display, providing greater flexibility, the ability to use the same data in multiple views, and better performance when working with large data volumes.
What combination of flags does a list item need for users to select, edit, and drag it?Answer
Correct answer: Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled. Each flag controls a specific interaction capability.
Why isn’t simply calling sortItems() enough for sorting items by dates or numeric values?Answer
Correct answer: By default, sortItems() uses alphabetical (lexicographic) string sorting. For correct sorting by dates or numbers, you need to inherit the item class and override operator<() to compare values correctly.
In what situations is QComboBox preferable to QListWidget, even if they’re functionally similar?Answer
Correct answer: When space conservation in the interface is important and users need to select only one item from the list. QComboBox shows only the current selection, expanding only when selecting.
Why use checkbox representation via Qt::ItemIsUserCheckable instead of adding an actual QCheckBox widget?Answer
Correct answer: This significantly improves list performance, especially with many items (hundreds or more). Checkbox representation doesn’t create an actual widget, just renders its appearance.
What’s the difference between activated() and currentIndexChanged() signals in QComboBox?Answer
Correct answer: activated() is emitted on every selection, even if users select the same item again. currentIndexChanged() is emitted only when the current item actually changes.
What happens if you call sortItems() and then add new items to the list with addItem()?Answer
Correct answer: New items won’t be automatically sorted and will be added to the end of the list. You’ll need to call sortItems() again to sort all items.
How do you set multiple selection mode for QListWidget or QTreeWidget?Answer
Correct answer: Call setSelectionMode() with parameter QAbstractItemView::MultiSelection. NoSelection prohibits selection, while SingleSelection allows selecting only one item.
Which method returns the list of all selected items in QListWidget when multiple selection mode is enabled?Answer
Correct answer: The selectedItems() method returns a list of all selected items. To get one current item, use currentItem().
Why is QTreeWidgetItemIterator used in QTreeWidget, and what flag should you pass to iterate only selected items?Answer
Correct answer: The iterator allows easily traversing all tree items, including nested ones. To iterate only selected items, pass the QTreeWidgetItemIterator::Selected flag to the iterator constructor.
What’s the purpose of the QTabWidget class, and in what situations is its use most justified?Answer
Correct answer: QTabWidget breaks down a complex dialog window with many options into a series of logically grouped simple windows, making the interface more understandable and user-friendly.
How do you check if a checkbox is set in a hierarchical list item with checkbox representation?Answer
Correct answer: Call the checkState() method with the column number and compare the result with Qt::Checked: if (ptwi->checkState(0) == Qt::Checked).

Hands-On Assignments

Easy Level

Directory with Categories
Create an application with a dropdown list (QComboBox) containing categories: “Fruits”, “Vegetables”, “Berries”. When a category is selected, the simple list (QListWidget) should display corresponding items. For example, for “Fruits”: apple, banana, orange. Add icons to list items.
Hints: Use the currentIndexChanged() signal from QComboBox to track category selection. When changing categories, clear the list with clear() and fill with new items. For icons, use QListWidgetItem::setIcon(). Store data in QMap<QString, QStringList> for convenience.
Medium Level

Task Manager with Priorities
Develop a task manager based on QTableWidget with three columns: “Task”, “Priority”, “Status”. Users should be able to add new tasks via QLineEdit, select priority from QComboBox (low/medium/high), and mark completion via checkbox in the cell. Implement sorting by priority when clicking column header. Add a button to delete selected row.
Hints: Use setSortingEnabled(true) for the table. For checkboxes, apply setCheckState() to QTableWidgetItem. For row deletion, use removeRow() with index from currentRow(). For custom priority sorting, create a QTableWidgetItem descendant with overloaded operator<().
Hard Level

File Navigator with Drag & Drop
Create a two-panel file navigator using QTreeWidget. The left panel shows folder hierarchy (minimum 3 nesting levels), the right panel shows selected folder contents in icon mode (QListWidget with IconMode). Implement file dragging between panels with visual indication. Double-clicking a folder in the right panel should open it in the left panel. Add context menu with “Create Folder”, “Rename”, “Delete” options. Use QTabWidget to organize multiple open folders.
Hints: Enable drag & drop via setDragEnabled(true) and setAcceptDrops(true). Handle dropEvent() to react to dragging. Use itemClicked() signal for panel synchronization. For context menu, create QMenu and show it in contextMenuEvent() handler. Store folder path in QTreeWidgetItem via setData(). For tabs, use addTab() with unique panel instances.

💬 Join the Discussion!

Figured out selection widgets? Have questions about when to use QListWidget versus when it’s better to switch to model-view architecture?

Share your solutions to hands-on assignments, discuss nuances of list performance optimization, and help other readers master the chapter material!

Your experience and questions help the entire community better understand Qt!

Leave a Reply

Your email address will not be published. Required fields are marked *