diff options
| author | Oleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io> | 2025-08-22 09:43:18 +0200 |
|---|---|---|
| committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2025-08-29 14:36:35 +0000 |
| commit | aa3f9f5dcabdd69e3db7345898335132840901d1 (patch) | |
| tree | ecd00e19eae064b75c15dd163b7189f19d72f480 /src/widgets/doc/snippets/code | |
| parent | f6435378d43d97e1e26b8ad5e5fc5c560fbc077a (diff) | |
Widgets snippets: Fix snippets and add to the build system
Created a new CMakeLists.txt for the widgets snippets code directory.
Added snippets to the build system and fixed related issues.
Task-number: QTBUG-137566
Change-Id: I1ec39266c6c0d14e513ffa604374ae2789d06b52
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/doc/snippets/code')
| -rw-r--r-- | src/widgets/doc/snippets/code/CMakeLists.txt | 45 | ||||
| -rw-r--r-- | src/widgets/doc/snippets/code/doc_src_model-view-programming.cpp | 96 | ||||
| -rw-r--r-- | src/widgets/doc/snippets/code/doc_src_qt4-mainwindow.cpp | 37 | ||||
| -rw-r--r-- | src/widgets/doc/snippets/code/doc_src_qt4-styles.cpp | 35 | ||||
| -rw-r--r-- | src/widgets/doc/snippets/code/doc_src_styles.cpp | 211 | ||||
| -rw-r--r-- | src/widgets/doc/snippets/code/doc_src_stylesheet.cpp | 166 |
6 files changed, 389 insertions, 201 deletions
diff --git a/src/widgets/doc/snippets/code/CMakeLists.txt b/src/widgets/doc/snippets/code/CMakeLists.txt new file mode 100644 index 00000000000..931e439f540 --- /dev/null +++ b/src/widgets/doc/snippets/code/CMakeLists.txt @@ -0,0 +1,45 @@ +# Copyright (C) 2025 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +add_library(widgets_snippets_code OBJECT + doc_src_layout.cpp + doc_src_qt4-styles.cpp +) + +target_link_libraries(widgets_snippets_code PRIVATE + Qt::Core + Qt::Gui + Qt::Widgets +) + +if(QT_FEATURE_itemmodel) + target_sources(widgets_snippets_code PRIVATE + doc_src_model-view-programming.cpp + ) +endif() + +if(QT_FEATURE_toolbar) + target_sources(widgets_snippets_code PRIVATE + doc_src_qt4-mainwindow.cpp + ) +endif() + +if(QT_FEATURE_checkbox) + target_sources(widgets_snippets_code PRIVATE + doc_src_styles.cpp + ) +endif() + +if(QT_FEATURE_lineedit) + target_sources(widgets_snippets_code PRIVATE + doc_src_stylesheet.cpp + ) +endif() + +set_target_properties(widgets_snippets_code PROPERTIES COMPILE_OPTIONS "-w") + +if ("${CMAKE_CXX_COMPILE_FEATURES}" MATCHES "cxx_std_23") + set_property(TARGET widgets_snippets_code PROPERTY CXX_STANDARD 23) +endif() + +set_target_properties(widgets_snippets_code PROPERTIES UNITY_BUILD OFF) diff --git a/src/widgets/doc/snippets/code/doc_src_model-view-programming.cpp b/src/widgets/doc/snippets/code/doc_src_model-view-programming.cpp index f63578f66ef..a111b1acd30 100644 --- a/src/widgets/doc/snippets/code/doc_src_model-view-programming.cpp +++ b/src/widgets/doc/snippets/code/doc_src_model-view-programming.cpp @@ -1,39 +1,63 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -QAbstractItemModel *model = index.model(); -//! [0] - - -//! [1] -QModelIndex index = model->index(row, column, ...); -//! [1] - - -//! [2] -QModelIndex indexA = model->index(0, 0, QModelIndex()); -QModelIndex indexB = model->index(1, 1, QModelIndex()); -QModelIndex indexC = model->index(2, 1, QModelIndex()); -//! [2] - - -//! [3] -QModelIndex index = model->index(row, column, parent); -//! [3] - - -//! [4] -QModelIndex indexA = model->index(0, 0, QModelIndex()); -QModelIndex indexC = model->index(2, 1, QModelIndex()); -//! [4] - - -//! [5] -QModelIndex indexB = model->index(1, 0, indexA); -//! [5] - - -//! [6] -QVariant value = model->data(index, role); -//! [6] +#include <QAbstractItemModel> +#include <QModelIndex> +#include <QVariant> + +void examples() +{ + QModelIndex index; + + //! [0] + const QAbstractItemModel *model = index.model(); + //! [0] + + int row, column; + + { + //! [1] + QModelIndex index = model->index(row, column /*...*/); + //! [1] + } + + { + //! [2] + QModelIndex indexA = model->index(0, 0, QModelIndex()); + QModelIndex indexB = model->index(1, 1, QModelIndex()); + QModelIndex indexC = model->index(2, 1, QModelIndex()); + //! [2] + } + + { + QModelIndex parent; + + //! [3] + QModelIndex index = model->index(row, column, parent); + //! [3] + } + + { + //! [4] + QModelIndex indexA = model->index(0, 0, QModelIndex()); + QModelIndex indexC = model->index(2, 1, QModelIndex()); + //! [4] + } + + { + QModelIndex indexA; + + //! [5] + QModelIndex indexB = model->index(1, 0, indexA); + //! [5] + } + + { + QModelIndex index; + int role = Qt::DisplayRole; + + //! [6] + QVariant value = model->data(index, role); + //! [6] + } +} diff --git a/src/widgets/doc/snippets/code/doc_src_qt4-mainwindow.cpp b/src/widgets/doc/snippets/code/doc_src_qt4-mainwindow.cpp index 2c8a8925118..5566496f8f9 100644 --- a/src/widgets/doc/snippets/code/doc_src_qt4-mainwindow.cpp +++ b/src/widgets/doc/snippets/code/doc_src_qt4-mainwindow.cpp @@ -1,28 +1,37 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +#include <QtWidgets> + +#include "../include/mainwindow.h" + //! [0] MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { //! [0] +} +void MainWindow::setupContents() +{ + QToolBar *fileToolbar; -//! [1] -fileToolbar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); -addToolBar(Qt::TopToolBarArea, fileToolbar); -//! [1] + //! [1] + fileToolbar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea); + addToolBar(Qt::TopToolBarArea, fileToolbar); + //! [1] -//! [2] -setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); -setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); -setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea); -setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); -//! [2] + //! [2] + setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); + setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); + setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea); + setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); + //! [2] -//! [3] -QWidget *centralWidget = new QWidget(this); -setCentralWidget(centralWidget); -//! [3] + //! [3] + QWidget *centralWidget = new QWidget(this); + setCentralWidget(centralWidget); + //! [3] +} diff --git a/src/widgets/doc/snippets/code/doc_src_qt4-styles.cpp b/src/widgets/doc/snippets/code/doc_src_qt4-styles.cpp index 2ec3afba013..0c53e09a3b9 100644 --- a/src/widgets/doc/snippets/code/doc_src_qt4-styles.cpp +++ b/src/widgets/doc/snippets/code/doc_src_qt4-styles.cpp @@ -1,26 +1,39 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] -const QStyleOptionFocusRect *focusRectOption = - qstyleoption_cast<const QStyleOptionFocusRect *>(option); -if (focusRectOption) { - ... -} -//! [0] +#include <QStyleOptionFocusRect> +#include <QPainter> + +class MyWidget : public QWidget +{ + Q_OBJECT +public: + void paintEvent(QPaintEvent *event) override; +}; + +void snippet(const QStyleOptionFocusRect *option) +{ + //! [0] + const QStyleOptionFocusRect *focusRectOption = + qstyleoption_cast<const QStyleOptionFocusRect *>(option); + if (focusRectOption) { + //... + } + //! [0] +} //! [1] void MyWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); - ... + //... - QStyleOptionFocusRect option(1); - option.init(this); + QStyleOptionFocusRect option; + option.initFrom(this); option.backgroundColor = palette().color(QPalette::Window); - style().drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, + style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter, this); } //! [1] diff --git a/src/widgets/doc/snippets/code/doc_src_styles.cpp b/src/widgets/doc/snippets/code/doc_src_styles.cpp index ba91ac2b9ff..e40e4007a0f 100644 --- a/src/widgets/doc/snippets/code/doc_src_styles.cpp +++ b/src/widgets/doc/snippets/code/doc_src_styles.cpp @@ -1,94 +1,149 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [0] - opt.initFrom(q); - if (down) - opt.state |= QStyle::State_Sunken; - if (tristate && noChange) - opt.state |= QStyle::State_NoChange; - else - opt.state |= checked ? QStyle::State_On : - QStyle::State_Off; - if (q->testAttribute(Qt::WA_Hover) && q->underMouse()) { - if (hovering) - opt.state |= QStyle::State_MouseOver; - else - opt.state &= ~QStyle::State_MouseOver; - } - opt.text = text; - opt.icon = icon; - opt.iconSize = q->iconSize(); -//! [0] +#include <QStyle> +#include <QWidget> +#include <QStyleOptionButton> +#include <QStylePainter> +#include <QRect> +#include <QPalette> +#include <QFontMetrics> +#include <QCheckBox> +#include <QCommonStyle> +class CheckBox : public QCheckBox +{ +public: + void examples(); +}; -//! [1] - state = QStyle::State_None; - if (widget->isEnabled()) - state |= QStyle::State_Enabled; - if (widget->hasFocus()) - state |= QStyle::State_HasFocus; - if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange)) - state |= QStyle::State_KeyboardFocusChange; - if (widget->underMouse()) - state |= QStyle::State_MouseOver; - if (widget->window()->isActiveWindow()) - state |= QStyle::State_Active; -#ifdef QT_KEYPAD_NAVIGATION - if (widget->hasEditFocus()) - state |= QStyle::State_HasEditFocus; -#endif +class CommonStyle : public QCommonStyle +{ +public: + void examples(); +}; - direction = widget->layoutDirection(); - rect = widget->rect(); - palette = widget->palette(); - fontMetrics = widget->fontMetrics(); -//! [1] +void CheckBox::examples() +{ + { + QWidget *q; + QStyleOptionButton opt; + bool down, tristate, noChange, checked, hovering; + const QString text; + const QIcon icon; + //! [0] + opt.initFrom(q); + if (down) + opt.state |= QStyle::State_Sunken; + if (tristate && noChange) + opt.state |= QStyle::State_NoChange; + else + opt.state |= checked ? QStyle::State_On : QStyle::State_Off; + if (q->testAttribute(Qt::WA_Hover) && q->underMouse()) { + if (hovering) + opt.state |= QStyle::State_MouseOver; + else + opt.state &= ~QStyle::State_MouseOver; + } + opt.text = text; + opt.icon = icon; + opt.iconSize = q->size(); + //! [0] + } -//! [2] - QStylePainter p(this); - QStyleOptionButton opt = d->getStyleOption(); - p.drawControl(QStyle::CE_CheckBox, opt); -//! [2] - + { + QStyle::State state; + QWidget *widget; + Qt::LayoutDirection direction; + QRect rect; + QPalette palette; + QFontMetrics fontMetrics = widget->fontMetrics(); -//! [3] - QStyleOptionButton subopt = *btn; - subopt.rect = subElementRect(SE_CheckBoxIndicator, btn, widget); - drawPrimitive(PE_IndicatorCheckBox, &subopt, p, widget); - subopt.rect = subElementRect(SE_CheckBoxContents, btn, widget); - drawControl(CE_CheckBoxLabel, &subopt, p, widget); + //! [1] + state = QStyle::State_None; + if (widget->isEnabled()) + state |= QStyle::State_Enabled; + if (widget->hasFocus()) + state |= QStyle::State_HasFocus; + if (widget->window()->testAttribute(Qt::WA_KeyboardFocusChange)) + state |= QStyle::State_KeyboardFocusChange; + if (widget->underMouse()) + state |= QStyle::State_MouseOver; + if (widget->window()->isActiveWindow()) + state |= QStyle::State_Active; + #ifdef QT_KEYPAD_NAVIGATION + if (widget->hasEditFocus()) + state |= QStyle::State_HasEditFocus; + #endif - if (btn->state & State_HasFocus) { - QStyleOptionFocusRect fropt; - fropt.QStyleOption::operator=(*btn); - fropt.rect = subElementRect(SE_CheckBoxFocusRect, btn, widget); - drawPrimitive(PE_FrameFocusRect, &fropt, p, widget); + direction = widget->layoutDirection(); + rect = widget->rect(); + palette = widget->palette(); + fontMetrics = widget->fontMetrics(); + //! [1] } -//! [3] + { + QStyle *d; + //! [2] + QStylePainter p; + QStyleOptionButton opt; + initStyleOption(&opt); + p.drawControl(QStyle::CE_CheckBox, opt); + //! [2] + } +} -//! [4] - const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt); - uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter); +void CommonStyle::examples() +{ + { + QPainter *p; + QStyleOptionButton *btn; + QWidget *widget; + bool State_HasFocus; - if (!styleHint(SH_UnderlineShortcut, btn, widget)) - alignment |= Qt::TextHideMnemonic; - QPixmap pix; - QRect textRect = btn->rect; - if (!btn->icon.isNull()) { - const auto dpr = p->device()->devicePixelRatio(); - pix = btn->icon.pixmap(btn->iconSize, dpr, - btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled); - drawItemPixmap(p, btn->rect, alignment, pix); - if (btn->direction == Qt::RightToLeft) - textRect.setRight(textRect.right() - btn->iconSize.width() - 4); - else - textRect.setLeft(textRect.left() + btn->iconSize.width() + 4); + //! [3] + QStyleOptionButton subopt = *btn; + subopt.rect = subElementRect(QStyle::SE_CheckBoxIndicator, btn, widget); + drawPrimitive(QStyle::PE_IndicatorCheckBox, &subopt, p, widget); + subopt.rect = subElementRect(QStyle::SE_CheckBoxContents, btn, widget); + drawControl(QStyle::CE_CheckBoxLabel, &subopt, p, widget); + if (btn->state & State_HasFocus) { + QStyleOptionFocusRect fropt; + fropt.QStyleOption::operator=(*btn); + fropt.rect = subElementRect(QStyle::SE_CheckBoxFocusRect, btn, widget); + drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, p, widget); + } + //! [3] } - if (!btn->text.isEmpty()){ - drawItemText(p, textRect, alignment | Qt::TextShowMnemonic, - btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText); + + { + QStyleOptionButton *opt; + QWidget *widget; + QPainter *p; + + //! [4] + const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt); + uint alignment = visualAlignment(btn->direction, Qt::AlignLeft | Qt::AlignVCenter); + if (!styleHint(SH_UnderlineShortcut, btn, widget)) + alignment |= Qt::TextHideMnemonic; + QPixmap pix; + QRect textRect = btn->rect; + if (!btn->icon.isNull()) { + const auto dpr = p->device()->devicePixelRatio(); + pix = btn->icon.pixmap(btn->iconSize, dpr, + btn->state & State_Enabled ? QIcon::Normal : QIcon::Disabled); + drawItemPixmap(p, btn->rect, alignment, pix); + if (btn->direction == Qt::RightToLeft) + textRect.setRight(textRect.right() - btn->iconSize.width() - 4); + else + textRect.setLeft(textRect.left() + btn->iconSize.width() + 4); + } + if (!btn->text.isEmpty()){ + drawItemText(p, textRect, alignment | Qt::TextShowMnemonic, + btn->palette, btn->state & State_Enabled, btn->text, QPalette::WindowText); + } + //! [4] } -//! [4] +} diff --git a/src/widgets/doc/snippets/code/doc_src_stylesheet.cpp b/src/widgets/doc/snippets/code/doc_src_stylesheet.cpp index 91851f8f07c..d45fb1f0fe8 100644 --- a/src/widgets/doc/snippets/code/doc_src_stylesheet.cpp +++ b/src/widgets/doc/snippets/code/doc_src_stylesheet.cpp @@ -1,52 +1,82 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -//! [21] -qApp->setStyleSheet("QPushButton { color: white }"); -//! [21] - - -//! [22] -myPushButton->setStyleSheet("* { color: blue }"); -//! [22] +#include <QtWidgets> +#include <QCoreApplication> +void examples() +{ + QPushButton *myPushButton; -//! [23] -myPushButton->setStyleSheet("color: blue"); -//! [23] + { + //! [21] + qApp->setStyleSheet("QPushButton { color: white }"); + //! [21] + } + { + //! [22] + myPushButton->setStyleSheet("* { color: blue }"); + //! [22] + } -//! [24] -qApp->setStyleSheet("QGroupBox { color: red; } "); -//! [24] + { + //! [23] + myPushButton->setStyleSheet("color: blue"); + //! [23] + } -//! [25] -qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }"); -//! [25] + { + //! [24] + qApp->setStyleSheet("QGroupBox { color: red; } "); + //! [24] + } + { + //! [25] + qApp->setStyleSheet("QGroupBox, QGroupBox * { color: red; }"); + //! [25] + } +} //! [26] class MyPushButton : public QPushButton { // ... -} +}; // ... -qApp->setStyleSheet("MyPushButton { background: yellow; }"); +void someFunction() +{ + qApp->setStyleSheet("MyPushButton { background: yellow; }"); + //... //! [26] - +} //! [27] namespace ns { class MyPushButton : public QPushButton { // ... - } + }; } // ... -qApp->setStyleSheet("ns--MyPushButton { background: yellow; }"); +void someMethod() +{ + qApp->setStyleSheet("ns--MyPushButton { background: yellow; }"); + //... //! [27] +} - +class CustomWidget : public QWidget +{ + Q_OBJECT +public: + void wrap(); + void paintEvent(QPaintEvent *event) override; +private: + QWidget *myDialog; + QWidget *nameEdit; +}; //! [32] void CustomWidget::paintEvent(QPaintEvent *) { @@ -57,51 +87,63 @@ void CustomWidget::paintEvent(QPaintEvent *) } //! [32] +void CustomWidget::wrap() +{ + { + //! [88] + qApp->setStyleSheet("QLineEdit { background-color: yellow }"); + //! [88] + } -//! [88] -qApp->setStyleSheet("QLineEdit { background-color: yellow }"); -//! [88] - - -//! [89] -myDialog->setStyleSheet("QLineEdit { background-color: yellow }"); -//! [89] - - -//! [90] -myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }"); -//! [90] - - -//! [91] -nameEdit->setStyleSheet("background-color: yellow"); -//! [91] - + { + //! [89] + myDialog->setStyleSheet("QLineEdit { background-color: yellow }"); + //! [89] + } -//! [92] -nameEdit->setStyleSheet("color: blue; background-color: yellow"); -//! [92] + { + //! [90] + myDialog->setStyleSheet("QLineEdit#nameEdit { background-color: yellow }"); + //! [90] + } + { + //! [91] + nameEdit->setStyleSheet("background-color: yellow"); + //! [91] + } -//! [93] -nameEdit->setStyleSheet("color: blue;" - "background-color: yellow;" - "selection-color: yellow;" - "selection-background-color: blue;"); -//! [93] + { + //! [92] + nameEdit->setStyleSheet("color: blue; background-color: yellow"); + //! [92] + } + { + //! [93] + nameEdit->setStyleSheet("color: blue;" + "background-color: yellow;" + "selection-color: yellow;" + "selection-background-color: blue;"); + //! [93] + } -//! [95] -QLineEdit *nameEdit = new QLineEdit(this); -nameEdit->setProperty("mandatoryField", true); + { + //! [95] + QLineEdit *nameEdit = new QLineEdit(this); + nameEdit->setProperty("mandatoryField", true); -QLineEdit *emailEdit = new QLineEdit(this); -emailEdit->setProperty("mandatoryField", true); + QLineEdit *emailEdit = new QLineEdit(this); + emailEdit->setProperty("mandatoryField", true); -QSpinBox *ageSpinBox = new QSpinBox(this); -ageSpinBox->setProperty("mandatoryField", true); -//! [95] + QSpinBox *ageSpinBox = new QSpinBox(this); + ageSpinBox->setProperty("mandatoryField", true); + //! [95] + } -//! [96] -QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true); -//! [96] + { + //! [96] + QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true); + //! [96] + } +} |
