summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets/tooltips/sortingbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/widgets/tooltips/sortingbox.cpp')
-rw-r--r--examples/widgets/widgets/tooltips/sortingbox.cpp39
1 files changed, 23 insertions, 16 deletions
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();
}