diff options
Diffstat (limited to 'examples/widgets')
21 files changed, 147 insertions, 51 deletions
diff --git a/examples/widgets/animation/easing/animation.h b/examples/widgets/animation/easing/animation.h index 696257bb4a..d78997d5ff 100644 --- a/examples/widgets/animation/easing/animation.h +++ b/examples/widgets/animation/easing/animation.h @@ -62,8 +62,8 @@ public: CirclePath, NPathTypes }; - Animation(QObject *target, const QByteArray &prop) - : QPropertyAnimation(target, prop) + Animation(QObject *target, const QByteArray &prop, QObject *parent = nullptr) + : QPropertyAnimation(target, prop, parent) { setPathType(LinearPath); } diff --git a/examples/widgets/animation/easing/window.cpp b/examples/widgets/animation/easing/window.cpp index 7b4fe1d912..d0deb843e4 100644 --- a/examples/widgets/animation/easing/window.cpp +++ b/examples/widgets/animation/easing/window.cpp @@ -82,7 +82,7 @@ Window::Window(QWidget *parent) m_scene.addItem(m_item); m_ui.graphicsView->setScene(&m_scene); - m_anim = new Animation(m_item, "pos"); + m_anim = new Animation(m_item, "pos", this); m_anim->setEasingCurve(QEasingCurve::OutBounce); m_ui.easingCurvePicker->setCurrentRow(int(QEasingCurve::OutBounce)); diff --git a/examples/widgets/animation/easing/window.h b/examples/widgets/animation/easing/window.h index 0c49dd6e8a..51c7792e19 100644 --- a/examples/widgets/animation/easing/window.h +++ b/examples/widgets/animation/easing/window.h @@ -48,6 +48,9 @@ ** ****************************************************************************/ +#ifndef WINDOW_H +#define WINDOW_H + #include <QtWidgets> #include "ui_form.h" @@ -84,3 +87,5 @@ private: Animation *m_anim; QSize m_iconSize; }; + +#endif // WINDOW_H diff --git a/examples/widgets/doc/src/tooltips.qdoc b/examples/widgets/doc/src/tooltips.qdoc index a278215503..da3f1bd489 100644 --- a/examples/widgets/doc/src/tooltips.qdoc +++ b/examples/widgets/doc/src/tooltips.qdoc @@ -148,6 +148,10 @@ private \c createShapeItem(), \c initialItemPosition() and \c initialItemColor() functions. + \snippet widgets/tooltips/sortingbox.cpp 27 + + In the destructor, we delete all shape items. + \snippet widgets/tooltips/sortingbox.cpp 5 QWidget::event() is the main event handler and receives all the @@ -223,8 +227,9 @@ If an item covers the position, we store a pointer to that item and the event's position. If several of the shape items cover the position, we store the pointer to the uppermost item. Finally, we - move the shape item to the end of the list, and make a call to the - QWidget::update() function to make the item appear on top. + move the shape item's pointer to the end of the list, and make + a call to the QWidget::update() function to make the item appear + on top. The QWidget::update() function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt @@ -314,10 +319,9 @@ The \c createShapeItem() function creates a single shape item. It sets the path, tooltip, position and color, using the item's own - functions. In the end, the function appends the new item to the - list of shape items, and calls the QWidget::update() function to - make it appear with the other items within the \c SortingBox - widget. + functions. In the end, the function appends the new item's pointer + to the list of shape items, and calls QWidget::update() to make + it appear with the other items within the \c SortingBox widget. \snippet widgets/tooltips/sortingbox.cpp 22 diff --git a/examples/widgets/graphicsview/flowlayout/flowlayout.h b/examples/widgets/graphicsview/flowlayout/flowlayout.h index 14251072ec..d8ec253792 100644 --- a/examples/widgets/graphicsview/flowlayout/flowlayout.h +++ b/examples/widgets/graphicsview/flowlayout/flowlayout.h @@ -48,6 +48,9 @@ ** ****************************************************************************/ +#ifndef FLOWLAYOUT_H +#define FLOWLAYOUT_H + #include <QGraphicsLayout> class FlowLayout : public QGraphicsLayout @@ -84,3 +87,5 @@ inline void FlowLayout::addItem(QGraphicsLayoutItem *item) { insertItem(-1, item); } + +#endif // FLOWLAYOUT_H diff --git a/examples/widgets/graphicsview/flowlayout/window.h b/examples/widgets/graphicsview/flowlayout/window.h index 24a7cf908b..9323125fd4 100644 --- a/examples/widgets/graphicsview/flowlayout/window.h +++ b/examples/widgets/graphicsview/flowlayout/window.h @@ -48,6 +48,9 @@ ** ****************************************************************************/ +#ifndef WINDOW_H +#define WINDOW_H + #include <QGraphicsWidget> class Window : public QGraphicsWidget @@ -56,3 +59,5 @@ class Window : public QGraphicsWidget public: Window(QGraphicsItem *parent = nullptr); }; + +#endif // WINDOW_H diff --git a/examples/widgets/layouts/basiclayouts/dialog.cpp b/examples/widgets/layouts/basiclayouts/dialog.cpp index 8376820545..d6d228920f 100644 --- a/examples/widgets/layouts/basiclayouts/dialog.cpp +++ b/examples/widgets/layouts/basiclayouts/dialog.cpp @@ -83,9 +83,24 @@ Dialog::Dialog() mainLayout->addWidget(formGroupBox); mainLayout->addWidget(bigEditor); mainLayout->addWidget(buttonBox); -//! [4] //! [5] - setLayout(mainLayout); +//! [4] + QWidget *scrollAreaContent = new QWidget; + scrollAreaContent->setLayout(mainLayout); + QScrollArea *scrollArea = new QScrollArea; + scrollArea->setFrameShape(QFrame::NoFrame); + scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + scrollArea->setWidgetResizable(true); + scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width: 20px;}"); + + scrollArea->setWidget(scrollAreaContent); + + QVBoxLayout *scrollLayout = new QVBoxLayout; + scrollLayout->setContentsMargins(0,0,0,0); + scrollLayout->addWidget(scrollArea); + +//! [5] + setLayout(scrollLayout); setWindowTitle(tr("Basic Layouts")); } //! [5] diff --git a/examples/widgets/painting/composition/composition.cpp b/examples/widgets/painting/composition/composition.cpp index a220fb42fa..cc055e63bc 100644 --- a/examples/widgets/painting/composition/composition.cpp +++ b/examples/widgets/painting/composition/composition.cpp @@ -266,6 +266,7 @@ CompositionRenderer::CompositionRenderer(QWidget *parent) setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); #if QT_CONFIG(opengl) m_pbuffer_size = 1024; + m_base_tex = 0; #endif } @@ -361,6 +362,7 @@ void CompositionRenderer::paint(QPainter *painter) { #if QT_CONFIG(opengl) if (usesOpenGL() && glWindow()->isValid()) { + auto *funcs = QOpenGLContext::currentContext()->functions(); if (!m_blitter.isCreated()) m_blitter.create(); @@ -385,10 +387,13 @@ void CompositionRenderer::paint(QPainter *painter) p.setCompositionMode(QPainter::CompositionMode_SourceOver); drawBase(p); p.end(); + if (m_base_tex) + funcs->glDeleteTextures(1, &m_base_tex); m_base_tex = m_fbo->takeTexture(); } painter->beginNativePainting(); + uint compositingTex; { QPainter p(m_fbo.get()); p.beginNativePainting(); @@ -400,19 +405,18 @@ void CompositionRenderer::paint(QPainter *painter) p.endNativePainting(); drawSource(p); p.end(); - m_compositing_tex = m_fbo->takeTexture(); + compositingTex = m_fbo->texture(); } painter->endNativePainting(); painter->beginNativePainting(); - auto *funcs = QOpenGLContext::currentContext()->functions(); funcs->glEnable(GL_BLEND); funcs->glBlendEquation(GL_FUNC_ADD); funcs->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); m_blitter.bind(); const QRect targetRect(QPoint(0, 0), m_fbo->size()); const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), size())); - m_blitter.blit(m_compositing_tex, target, QOpenGLTextureBlitter::OriginBottomLeft); + m_blitter.blit(compositingTex, target, QOpenGLTextureBlitter::OriginBottomLeft); m_blitter.release(); painter->endNativePainting(); } else diff --git a/examples/widgets/painting/composition/composition.h b/examples/widgets/painting/composition/composition.h index 52ca7919b0..88bc50dc80 100644 --- a/examples/widgets/painting/composition/composition.h +++ b/examples/widgets/painting/composition/composition.h @@ -195,7 +195,6 @@ private: std::unique_ptr<QFboPaintDevice> m_fbo; int m_pbuffer_size; // width==height==size of pbuffer uint m_base_tex; - uint m_compositing_tex; QSize m_previous_size; QOpenGLTextureBlitter m_blitter; #endif diff --git a/examples/widgets/painting/shared/fbopaintdevice.cpp b/examples/widgets/painting/shared/fbopaintdevice.cpp index 9368293218..b7e7073501 100644 --- a/examples/widgets/painting/shared/fbopaintdevice.cpp +++ b/examples/widgets/painting/shared/fbopaintdevice.cpp @@ -71,11 +71,13 @@ QFboPaintDevice::QFboPaintDevice(const QSize &size, bool flipped, bool clearOnIn context()->functions()->glClearColor(0, 0, 0, 0); context()->functions()->glClear(GL_COLOR_BUFFER_BIT); } + m_resolvedFbo = new QOpenGLFramebufferObject(m_framebufferObject->size(), m_framebufferObject->attachment()); } QFboPaintDevice::~QFboPaintDevice() { delete m_framebufferObject; + delete m_resolvedFbo; delete m_surface; } @@ -87,12 +89,19 @@ void QFboPaintDevice::ensureActiveTarget() m_framebufferObject->bind(); } +GLuint QFboPaintDevice::texture() +{ + m_resolvedFbo->bind(); // to get the backing texture recreated if it was taken (in takeTexture) previously + QOpenGLFramebufferObject::blitFramebuffer(m_resolvedFbo, m_framebufferObject); + return m_resolvedFbo->texture(); +} + GLuint QFboPaintDevice::takeTexture() { - // We have multisamples so we can't just forward takeTexture(). - QOpenGLFramebufferObject resolvedFbo(m_framebufferObject->size(), m_framebufferObject->attachment()); - QOpenGLFramebufferObject::blitFramebuffer(&resolvedFbo, m_framebufferObject); - return resolvedFbo.takeTexture(); + m_resolvedFbo->bind(); // to get the backing texture recreated if it was taken (in takeTexture) previously + // We have multisamples so we can't just forward takeTexture(), have to resolve first. + QOpenGLFramebufferObject::blitFramebuffer(m_resolvedFbo, m_framebufferObject); + return m_resolvedFbo->takeTexture(); } QImage QFboPaintDevice::toImage() const diff --git a/examples/widgets/painting/shared/fbopaintdevice.h b/examples/widgets/painting/shared/fbopaintdevice.h index a42bcc756d..e2dd31427b 100644 --- a/examples/widgets/painting/shared/fbopaintdevice.h +++ b/examples/widgets/painting/shared/fbopaintdevice.h @@ -69,6 +69,7 @@ public: bool isValid() const { return m_framebufferObject->isValid(); } GLuint handle() const { return m_framebufferObject->handle(); } + GLuint texture(); GLuint takeTexture(); QImage toImage() const; @@ -83,6 +84,7 @@ public: private: QOpenGLFramebufferObject *m_framebufferObject; + QOpenGLFramebufferObject *m_resolvedFbo; QSurface *m_surface; }; diff --git a/examples/widgets/tools/echoplugin/CMakeLists.txt b/examples/widgets/tools/echoplugin/CMakeLists.txt index 6e32a4de09..69bb6e981a 100644 --- a/examples/widgets/tools/echoplugin/CMakeLists.txt +++ b/examples/widgets/tools/echoplugin/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.16) project(plugandpaint LANGUAGES CXX) -find_package(Qt6 COMPONENTS Core Gui Widgets) - set(CMAKE_AUTOMOC ON) if(NOT DEFINED INSTALL_EXAMPLESDIR) @@ -11,5 +9,7 @@ endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/echoplugin") +find_package(Qt6 COMPONENTS Core Gui Widgets) + add_subdirectory(plugin) add_subdirectory(echowindow) diff --git a/examples/widgets/tools/plugandpaint/CMakeLists.txt b/examples/widgets/tools/plugandpaint/CMakeLists.txt index e681812dad..cc1dae43ff 100644 --- a/examples/widgets/tools/plugandpaint/CMakeLists.txt +++ b/examples/widgets/tools/plugandpaint/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.16) project(plugandpaint LANGUAGES CXX) -find_package(Qt6 COMPONENTS Core Gui Widgets) - set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) @@ -13,5 +11,7 @@ endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/plugandpaint") +find_package(Qt6 COMPONENTS Core Gui Widgets) + add_subdirectory(plugins) add_subdirectory(app) diff --git a/examples/widgets/tools/styleplugin/CMakeLists.txt b/examples/widgets/tools/styleplugin/CMakeLists.txt index 33412f5306..31c49cc754 100644 --- a/examples/widgets/tools/styleplugin/CMakeLists.txt +++ b/examples/widgets/tools/styleplugin/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.16) project(styleplugin LANGUAGES CXX) -find_package(Qt6 COMPONENTS Widgets) - set(CMAKE_AUTOMOC ON) if(NOT DEFINED INSTALL_EXAMPLESDIR) @@ -11,5 +9,7 @@ endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/tools/styleplugin") +find_package(Qt6 COMPONENTS Widgets) + add_subdirectory(stylewindow) add_subdirectory(plugin) diff --git a/examples/widgets/widgets/groupbox/window.cpp b/examples/widgets/widgets/groupbox/window.cpp index 95c4f216fe..6a939d4f7d 100644 --- a/examples/widgets/widgets/groupbox/window.cpp +++ b/examples/widgets/widgets/groupbox/window.cpp @@ -183,12 +183,10 @@ QGroupBox *Window::createPushButtonGroup() popupButton->setMenu(menu); //! [12] - QAction *newAction = menu->addAction(tr("Submenu")); - QMenu *subMenu = new QMenu(tr("Popup Submenu")); + QMenu *subMenu = menu->addMenu(tr("Submenu")); subMenu->addAction(tr("Item 1")); subMenu->addAction(tr("Item 2")); subMenu->addAction(tr("Item 3")); - newAction->setMenu(subMenu); //! [13] QVBoxLayout *vbox = new QVBoxLayout; diff --git a/examples/widgets/widgets/sliders/window.cpp b/examples/widgets/widgets/sliders/window.cpp index 916f206379..fc6ac6f193 100644 --- a/examples/widgets/widgets/sliders/window.cpp +++ b/examples/widgets/widgets/sliders/window.cpp @@ -50,10 +50,8 @@ #include "slidersgroup.h" #include "window.h" - #include <QCheckBox> #include <QComboBox> -#include <QHBoxLayout> #include <QLabel> #include <QSpinBox> #include <QStackedWidget> @@ -81,9 +79,10 @@ Window::Window(QWidget *parent) connect(valueSpinBox, &QSpinBox::valueChanged, horizontalSliders, &SlidersGroup::setValue); - QHBoxLayout *layout = new QHBoxLayout; - layout->addWidget(controlsGroup); - layout->addWidget(stackedWidget); + layout = new QGridLayout; + layout->addWidget(stackedWidget, 0, 1); + layout->addWidget(controlsGroup, 0, 0); + setLayout(layout); minimumSpinBox->setValue(0); @@ -157,5 +156,37 @@ void Window::createControls(const QString &title) controlsLayout->addWidget(invertedKeyBindings, 1, 2); controlsLayout->addWidget(orientationCombo, 3, 0, 1, 3); controlsGroup->setLayout(controlsLayout); + } //! [8] + + +void Window::resizeEvent(QResizeEvent *e) +{ + if (width() == 0 || height() == 0) + return; + + const double aspectRatio = double(width()) / double(height()); + + if ((aspectRatio < 1.0) && (oldAspectRatio > 1.0)) { + layout->removeWidget(controlsGroup); + layout->removeWidget(stackedWidget); + + layout->addWidget(stackedWidget, 1, 0); + layout->addWidget(controlsGroup, 0, 0); + + oldAspectRatio = aspectRatio; + } + else if ((aspectRatio > 1.0) && (oldAspectRatio < 1.0)) { + layout->removeWidget(controlsGroup); + layout->removeWidget(stackedWidget); + + layout->addWidget(stackedWidget, 0, 1); + layout->addWidget(controlsGroup, 0, 0); + + oldAspectRatio = aspectRatio; + } +} + + + diff --git a/examples/widgets/widgets/sliders/window.h b/examples/widgets/widgets/sliders/window.h index 4894781ac2..7c2297bd83 100644 --- a/examples/widgets/widgets/sliders/window.h +++ b/examples/widgets/widgets/sliders/window.h @@ -52,6 +52,7 @@ #define WINDOW_H #include <QWidget> +#include <QGridLayout> QT_BEGIN_NAMESPACE class QCheckBox; @@ -73,6 +74,7 @@ public: private: void createControls(const QString &title); + void resizeEvent(QResizeEvent *e); SlidersGroup *horizontalSliders; SlidersGroup *verticalSliders; @@ -88,6 +90,8 @@ private: QSpinBox *maximumSpinBox; QSpinBox *valueSpinBox; QComboBox *orientationCombo; + QGridLayout *layout; + double oldAspectRatio; }; //! [0] diff --git a/examples/widgets/widgets/tooltips/sortingbox.cpp b/examples/widgets/widgets/tooltips/sortingbox.cpp index d993b098b2..733e567873 100644 --- a/examples/widgets/widgets/tooltips/sortingbox.cpp +++ b/examples/widgets/widgets/tooltips/sortingbox.cpp @@ -106,6 +106,13 @@ SortingBox::SortingBox(QWidget *parent) } //! [4] +//! [27] +SortingBox::~SortingBox() +{ + qDeleteAll(shapeItems); +} +//! [27] + //! [5] bool SortingBox::event(QEvent *event) { @@ -114,7 +121,7 @@ bool SortingBox::event(QEvent *event) QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event); int index = itemAt(helpEvent->pos()); if (index != -1) { - QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip()); + QToolTip::showText(helpEvent->globalPos(), shapeItems[index]->toolTip()); } else { QToolTip::hideText(); event->ignore(); @@ -144,13 +151,13 @@ void SortingBox::paintEvent(QPaintEvent * /* event */) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - for (const ShapeItem &shapeItem : qAsConst(shapeItems)) { + for (const ShapeItem *shapeItem : std::as_const(shapeItems)) { //! [8] //! [9] - painter.translate(shapeItem.position()); + painter.translate(shapeItem->position()); //! [9] //! [10] - painter.setBrush(shapeItem.color()); - painter.drawPath(shapeItem.path()); - painter.translate(-shapeItem.position()); + painter.setBrush(shapeItem->color()); + painter.drawPath(shapeItem->path()); + painter.translate(-shapeItem->position()); } } //! [10] @@ -161,7 +168,7 @@ void SortingBox::mousePressEvent(QMouseEvent *event) if (event->button() == Qt::LeftButton) { int index = itemAt(event->position().toPoint()); if (index != -1) { - itemInMotion = &shapeItems[index]; + itemInMotion = shapeItems[index]; previousPosition = event->position().toPoint(); shapeItems.move(index, shapeItems.size() - 1); update(); @@ -216,11 +223,11 @@ void SortingBox::createNewTriangle() //! [16] //! [17] -int SortingBox::itemAt(const QPoint &pos) +qsizetype SortingBox::itemAt(const QPoint &pos) { - for (int i = shapeItems.size() - 1; i >= 0; --i) { - const ShapeItem &item = shapeItems[i]; - if (item.path().contains(pos - item.position())) + for (qsizetype i = shapeItems.size() - 1; i >= 0; --i) { + const ShapeItem *item = shapeItems[i]; + if (item->path().contains(pos - item->position())) return i; } return -1; @@ -255,11 +262,11 @@ void SortingBox::createShapeItem(const QPainterPath &path, const QString &toolTip, const QPoint &pos, const QColor &color) { - ShapeItem shapeItem; - shapeItem.setPath(path); - shapeItem.setToolTip(toolTip); - shapeItem.setPosition(pos); - shapeItem.setColor(color); + ShapeItem *shapeItem = new ShapeItem; + shapeItem->setPath(path); + shapeItem->setToolTip(toolTip); + shapeItem->setPosition(pos); + shapeItem->setColor(color); shapeItems.append(shapeItem); update(); } diff --git a/examples/widgets/widgets/tooltips/sortingbox.h b/examples/widgets/widgets/tooltips/sortingbox.h index 3d0cecea2b..90534ab13d 100644 --- a/examples/widgets/widgets/tooltips/sortingbox.h +++ b/examples/widgets/widgets/tooltips/sortingbox.h @@ -68,6 +68,7 @@ class SortingBox : public QWidget public: SortingBox(QWidget *parent = nullptr); + ~SortingBox(); protected: bool event(QEvent *event) override; @@ -88,7 +89,7 @@ private: int updateButtonGeometry(QToolButton *button, int x, int y); void createShapeItem(const QPainterPath &path, const QString &toolTip, const QPoint &pos, const QColor &color); - int itemAt(const QPoint &pos); + qsizetype itemAt(const QPoint &pos); void moveItemTo(const QPoint &pos); QPoint initialItemPosition(const QPainterPath &path); QPoint randomItemPosition(); @@ -99,7 +100,7 @@ private: const char *member); //! [2] - QList<ShapeItem> shapeItems; + QList<ShapeItem *> shapeItems; QPainterPath circlePath; QPainterPath squarePath; QPainterPath trianglePath; diff --git a/examples/widgets/widgets/validators/validatorwidget.h b/examples/widgets/widgets/validators/validatorwidget.h index bcc4a9b91e..7b80c58504 100644 --- a/examples/widgets/widgets/validators/validatorwidget.h +++ b/examples/widgets/widgets/validators/validatorwidget.h @@ -48,6 +48,9 @@ ** ****************************************************************************/ +#ifndef VALIDATORWIDGET_H +#define VALIDATORWIDGET_H + #include <QWidget> #include "ui_validators.h" @@ -62,3 +65,5 @@ private slots: void updateValidator(); void updateDoubleValidator(); }; + +#endif // VALIDATORWIDGET_H diff --git a/examples/widgets/widgets/windowflags/previewwindow.cpp b/examples/widgets/widgets/windowflags/previewwindow.cpp index d7ebed7b3c..c05e486c1d 100644 --- a/examples/widgets/widgets/windowflags/previewwindow.cpp +++ b/examples/widgets/widgets/windowflags/previewwindow.cpp @@ -124,6 +124,8 @@ void PreviewWindow::setWindowFlags(Qt::WindowFlags flags) text += "\n| Qt::WindowShadeButtonHint"; if (flags & Qt::WindowStaysOnTopHint) text += "\n| Qt::WindowStaysOnTopHint"; + if (flags & Qt::WindowStaysOnBottomHint) + text += "\n| Qt::WindowStaysOnBottomHint"; if (flags & Qt::CustomizeWindowHint) text += "\n| Qt::CustomizeWindowHint"; |