diff options
Diffstat (limited to 'examples/widgets')
-rw-r--r-- | examples/widgets/animation/easing/window.h | 5 | ||||
-rw-r--r-- | examples/widgets/doc/src/tooltips.qdoc | 16 | ||||
-rw-r--r-- | examples/widgets/graphicsview/flowlayout/flowlayout.h | 5 | ||||
-rw-r--r-- | examples/widgets/graphicsview/flowlayout/window.h | 5 | ||||
-rw-r--r-- | examples/widgets/painting/composition/composition.cpp | 10 | ||||
-rw-r--r-- | examples/widgets/painting/composition/composition.h | 1 | ||||
-rw-r--r-- | examples/widgets/painting/shared/fbopaintdevice.cpp | 17 | ||||
-rw-r--r-- | examples/widgets/painting/shared/fbopaintdevice.h | 2 | ||||
-rw-r--r-- | examples/widgets/widgets/tooltips/sortingbox.cpp | 39 | ||||
-rw-r--r-- | examples/widgets/widgets/tooltips/sortingbox.h | 5 | ||||
-rw-r--r-- | examples/widgets/widgets/validators/validatorwidget.h | 5 |
11 files changed, 78 insertions, 32 deletions
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/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/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 |