summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/qsignalmapper/buttonwidget.cpp
blob: cdbf83a977db346634dc3fd43a0db966664404df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "buttonwidget.h"

#include <QtWidgets>

    //! [OpenCtor]
    ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
        : QWidget(parent)
    {
    //! [OpenCtor]
    {
        //![OldNotation]
            signalMapper = new QSignalMapper(this);

            QGridLayout *gridLayout = new QGridLayout(this);
            for (int i = 0; i < texts.size(); ++i) {
                QPushButton *button = new QPushButton(texts[i]);
                connect(button, &QPushButton::clicked, signalMapper, qOverload<>(&QSignalMapper::map));
                signalMapper->setMapping(button, texts[i]);
                gridLayout->addWidget(button, i / 3, i % 3);
            }

            connect(signalMapper, &QSignalMapper::mappedString,
                    this, &ButtonWidget::clicked);
        //![OldNotation]
    }

    {
        //![ModernNotation]
            QGridLayout *gridLayout = new QGridLayout(this);
            for (int i = 0; i < texts.size(); ++i) {
                QString text = texts[i];
                QPushButton *button = new QPushButton(text);
                connect(button, &QPushButton::clicked, [this, text] { clicked(text); });
                gridLayout->addWidget(button, i / 3, i % 3);
            }
        //![ModernNotation]
    }

    //! [CloseBrackets]
    }
    //! [CloseBrackets]