summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2025-07-10 21:14:32 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2025-07-21 15:28:26 +0200
commit4d88d3941787847c969a6ace185717776f878044 (patch)
tree26fce17bced97d7878cb09fbd92ad80892db542b /src/corelib/doc/snippets
parent0f446ecdb2d50c0b3699b431933ccb1f9fe6b07b (diff)
QRangeModel: allow gadgets to declare themselves as MultiRole items
For Quick, gadgets in a list should be represented as single-column/ multi-role items. Wrapping the gadget type into a SingleColumn is possible, but messy, as it changes how the underlying data structure works. We can in addition allow the gadget type to declare itself directly as a multi-role item: by specializing a QRangeModel::RowOptions template, and giving it specific member types or constexpr values, types can modify how QRangeModel interprets them. This gives us a very flexible and extensible mechanism for future behaviors and compile-time optimizations. For now, check for a rowCategory member value, which can be set to MultiRoleItem to override the default interpretation as a multi-column row. Adjust test and documentation. Pick-to: 6.10 Change-Id: Icd2542ac2a9fb4abe7698bb4341b67053e01aa93 Reviewed-by: Artem Dyomin <artem.dyomin@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
-rw-r--r--src/corelib/doc/snippets/qrangemodel/main.cpp64
1 files changed, 58 insertions, 6 deletions
diff --git a/src/corelib/doc/snippets/qrangemodel/main.cpp b/src/corelib/doc/snippets/qrangemodel/main.cpp
index 4730a3c9ceb..a2f3f7c13e7 100644
--- a/src/corelib/doc/snippets/qrangemodel/main.cpp
+++ b/src/corelib/doc/snippets/qrangemodel/main.cpp
@@ -467,7 +467,7 @@ void color_map()
}
namespace multirole_gadget {
-//! [color_gadget_0]
+//! [color_gadget_decl]
class ColorEntry
{
Q_GADGET
@@ -475,6 +475,8 @@ class ColorEntry
Q_PROPERTY(QColor decoration READ decoration)
Q_PROPERTY(QString toolTip READ toolTip)
public:
+//! [color_gadget_decl]
+//! [color_gadget_impl]
ColorEntry(const QString &color = {})
: m_colorName(color)
{}
@@ -490,13 +492,39 @@ public:
private:
QString m_colorName;
+//! [color_gadget_impl]
+//! [color_gadget_end]
};
-//! [color_gadget_0]
+//! [color_gadget_end]
+}
+
+using ColorEntry = multirole_gadget::ColorEntry;
+//! [color_gadget_multi_role_gadget]
+
+template <>
+struct QRangeModel::RowOptions<ColorEntry>
+{
+ static constexpr auto rowCategory = QRangeModel::RowCategory::MultiRoleItem;
+};
+//! [color_gadget_multi_role_gadget]
+
+namespace multirole_gadget {
+void color_table() {
+ //! [color_gadget_table]
+ QList<QList<ColorEntry>> colorTable;
-void color_list() {
- //! [color_gadget_1]
+ // ...
+
+ QRangeModel colorModel(colorTable);
+ QTableView table;
+ table.setModel(&colorModel);
+ //! [color_gadget_table]
+}
+
+void color_list_multi_role() {
+ //! [color_gadget_multi_role]
const QStringList colorNames = QColor::colorNames();
- QList<QRangeModel::SingleColumn<ColorEntry>> colors;
+ QList<ColorEntry> colors;
colors.reserve(colorNames.size());
for (const QString &name : colorNames)
colors << name;
@@ -504,7 +532,31 @@ void color_list() {
QRangeModel colorModel(colors);
QListView list;
list.setModel(&colorModel);
- //! [color_gadget_1]
+ //! [color_gadget_multi_role]
+}
+
+void color_list_single_column() {
+ //! [color_gadget_single_column]
+ const QStringList colorNames = QColor::colorNames();
+ QList<QRangeModel::SingleColumn<ColorEntry>> colors;
+
+ // ...
+
+ QRangeModel colorModel(colors);
+ QListView list;
+ list.setModel(&colorModel);
+ //! [color_gadget_single_column]
+
+ {
+ //! [color_gadget_single_column_access_get]
+ ColorEntry firstEntry = std::get<0>(colors.at(0));
+ //! [color_gadget_single_column_access_get]
+ }
+ {
+ //! [color_gadget_single_column_access_sb]
+ auto [firstEntry] = colors.at(0);
+ //! [color_gadget_single_column_access_sb]
+ }
}
} // namespace multirole_gadget