diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2015-10-07 12:03:05 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> | 2015-10-09 15:59:21 +0000 |
commit | c8cd9f1b9a3d338a12bdccce957d74d515eb9c32 (patch) | |
tree | fca6b64875083ce22716c1ed3c6633459bfb93c2 /examples/widgets/draganddrop/draggabletext | |
parent | c55a36cb9015cf1eebd49eaa5b1b4f4ec9b28451 (diff) |
Polish DnD Examples.
- Remove class DragLabel in draggabletext and add a static creation
function instead since it only has a constructor setting some
properties.
- Use QRegularExpression instead of QRegExp
- Use new connection syntax.
- Ensure compilation with
DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
demonstrating use of QLatin1String vs QStringLiteral.
- Streamline code.
Change-Id: I2e2ddeb40837dba379990836c77fb72ad7263e2d
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
Diffstat (limited to 'examples/widgets/draganddrop/draggabletext')
4 files changed, 25 insertions, 133 deletions
diff --git a/examples/widgets/draganddrop/draggabletext/draggabletext.pro b/examples/widgets/draganddrop/draggabletext/draggabletext.pro index 2815be1613..1add2a270e 100644 --- a/examples/widgets/draganddrop/draggabletext/draggabletext.pro +++ b/examples/widgets/draganddrop/draggabletext/draggabletext.pro @@ -1,10 +1,8 @@ QT += widgets -HEADERS = draglabel.h \ - dragwidget.h +HEADERS = dragwidget.h RESOURCES = draggabletext.qrc -SOURCES = draglabel.cpp \ - dragwidget.cpp \ +SOURCES = dragwidget.cpp \ main.cpp # install diff --git a/examples/widgets/draganddrop/draggabletext/draglabel.cpp b/examples/widgets/draganddrop/draggabletext/draglabel.cpp deleted file mode 100644 index 4e741fb6c8..0000000000 --- a/examples/widgets/draganddrop/draggabletext/draglabel.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "draglabel.h" - -DragLabel::DragLabel(const QString &text, QWidget *parent) - : QLabel(text, parent) -{ - setAutoFillBackground(true); - setFrameShape(QFrame::Panel); - setFrameShadow(QFrame::Raised); -} diff --git a/examples/widgets/draganddrop/draggabletext/draglabel.h b/examples/widgets/draganddrop/draggabletext/draglabel.h deleted file mode 100644 index 8d2c31aa1d..0000000000 --- a/examples/widgets/draganddrop/draggabletext/draglabel.h +++ /dev/null @@ -1,58 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef DRAGLABEL_H -#define DRAGLABEL_H - -#include <QLabel> - -QT_BEGIN_NAMESPACE -class QDragEnterEvent; -class QDragMoveEvent; -class QFrame; -QT_END_NAMESPACE - -class DragLabel : public QLabel -{ -public: - DragLabel(const QString &text, QWidget *parent); -}; - -#endif // DRAGLABEL_H diff --git a/examples/widgets/draganddrop/draggabletext/dragwidget.cpp b/examples/widgets/draganddrop/draggabletext/dragwidget.cpp index 3b8bca3277..36c4df2e43 100644 --- a/examples/widgets/draganddrop/draggabletext/dragwidget.cpp +++ b/examples/widgets/draganddrop/draggabletext/dragwidget.cpp @@ -40,13 +40,23 @@ #include <QtWidgets> -#include "draglabel.h" #include "dragwidget.h" +static QLabel *createDragLabel(const QString &text, QWidget *parent) +{ + QLabel *label = new QLabel(text, parent); + label->setAutoFillBackground(true); + label->setFrameShape(QFrame::Panel); + label->setFrameShadow(QFrame::Raised); + return label; +} + +static QString hotSpotMimeDataKey() { return QStringLiteral("application/x-hotspot"); } + DragWidget::DragWidget(QWidget *parent) : QWidget(parent) { - QFile dictionaryFile(":/dictionary/words.txt"); + QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt")); dictionaryFile.open(QIODevice::ReadOnly); QTextStream inputStream(&dictionaryFile); @@ -57,7 +67,7 @@ DragWidget::DragWidget(QWidget *parent) QString word; inputStream >> word; if (!word.isEmpty()) { - DragLabel *wordLabel = new DragLabel(word, this); + QLabel *wordLabel = createDragLabel(word, this); wordLabel->move(x, y); wordLabel->show(); wordLabel->setAttribute(Qt::WA_DeleteOnClose); @@ -69,12 +79,6 @@ DragWidget::DragWidget(QWidget *parent) } } - /* - QPalette newPalette = palette(); - newPalette.setColor(QPalette::Window, Qt::white); - setPalette(newPalette); - */ - setAcceptDrops(true); setMinimumSize(400, qMax(200, y)); setWindowTitle(tr("Draggable Text")); @@ -98,19 +102,19 @@ void DragWidget::dropEvent(QDropEvent *event) { if (event->mimeData()->hasText()) { const QMimeData *mime = event->mimeData(); - QStringList pieces = mime->text().split(QRegExp("\\s+"), + QStringList pieces = mime->text().split(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts); QPoint position = event->pos(); QPoint hotSpot; - QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' '); + QByteArrayList hotSpotPos = mime->data(hotSpotMimeDataKey()).split(' '); if (hotSpotPos.size() == 2) { hotSpot.setX(hotSpotPos.first().toInt()); hotSpot.setY(hotSpotPos.last().toInt()); } - foreach (QString piece, pieces) { - DragLabel *newLabel = new DragLabel(piece, this); + foreach (const QString &piece, pieces) { + QLabel *newLabel = createDragLabel(piece, this); newLabel->move(position - hotSpot); newLabel->show(); newLabel->setAttribute(Qt::WA_DeleteOnClose); @@ -127,18 +131,15 @@ void DragWidget::dropEvent(QDropEvent *event) } else { event->ignore(); } - foreach (QObject *child, children()) { - if (child->inherits("QWidget")) { - QWidget *widget = static_cast<QWidget *>(child); - if (!widget->isVisible()) - widget->deleteLater(); - } + foreach (QWidget *widget, findChildren<QWidget *>()) { + if (!widget->isVisible()) + widget->deleteLater(); } } void DragWidget::mousePressEvent(QMouseEvent *event) { - QLabel *child = static_cast<QLabel*>(childAt(event->pos())); + QLabel *child = qobject_cast<QLabel*>(childAt(event->pos())); if (!child) return; @@ -146,8 +147,8 @@ void DragWidget::mousePressEvent(QMouseEvent *event) QMimeData *mimeData = new QMimeData; mimeData->setText(child->text()); - mimeData->setData("application/x-hotspot", - QByteArray::number(hotSpot.x()) + " " + QByteArray::number(hotSpot.y())); + mimeData->setData(hotSpotMimeDataKey(), + QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y())); QPixmap pixmap(child->size()); child->render(&pixmap); |