diff options
Diffstat (limited to 'tests/auto/quick/doc/how-tos/how-to-cpp-button')
5 files changed, 133 insertions, 0 deletions
diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-button/CMakeLists.txt b/tests/auto/quick/doc/how-tos/how-to-cpp-button/CMakeLists.txt new file mode 100644 index 0000000000..34f370c7b3 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-button/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_how-to-cpp-button LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_how-to-cpp-button + SOURCES + tst_how-to-cpp-button.cpp + LIBRARIES + Qt::Core + Qt::Gui + Qt::Quick + Qt::QuickPrivate + Qt::QuickControlsTestUtilsPrivate + Qt::QuickTemplates2Private +) + +qt_policy(SET QTP0001 NEW) + +qt_add_qml_module(tst_how-to-cpp-button + URI MyModule + QML_FILES + Main.qml + SOURCES + backend.h + backend.cpp +) diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-button/Main.qml b/tests/auto/quick/doc/how-tos/how-to-cpp-button/Main.qml new file mode 100644 index 0000000000..5150f6c6d6 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-button/Main.qml @@ -0,0 +1,19 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +//! [file] +import QtQuick.Controls + +import MyModule + +ApplicationWindow { + width: 400 + height: 400 + title: qsTr("C++ Button example") + + Button { + text: qsTr("Click me") + onClicked: Backend.doStuff() + } +} +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.cpp b/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.cpp new file mode 100644 index 0000000000..4143aea9e6 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.cpp @@ -0,0 +1,13 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +//! [file] +#include "backend.h" + +#include <QDebug> + +void Backend::doStuff() +{ + qDebug() << "Did stuff!"; +} +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.h b/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.h new file mode 100644 index 0000000000..10249f5416 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-button/backend.h @@ -0,0 +1,17 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +//! [file] +#include <QObject> +#include <QQmlEngine> + +class Backend : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + +public: + Q_INVOKABLE void doStuff(); +}; +//! [file] diff --git a/tests/auto/quick/doc/how-tos/how-to-cpp-button/tst_how-to-cpp-button.cpp b/tests/auto/quick/doc/how-tos/how-to-cpp-button/tst_how-to-cpp-button.cpp new file mode 100644 index 0000000000..792f84ffc2 --- /dev/null +++ b/tests/auto/quick/doc/how-tos/how-to-cpp-button/tst_how-to-cpp-button.cpp @@ -0,0 +1,53 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QtCore/qregularexpression.h> +#include <QtTest/QtTest> +#include <QtQml/qqmlapplicationengine.h> +#include <QtQuick/qquickwindow.h> +#include <QtQuickTemplates2/private/qquickbutton_p.h> +#include <QtQuickControlsTestUtils/private/controlstestutils_p.h> + +QT_BEGIN_NAMESPACE + +using namespace QQuickControlsTestUtils; + +class tst_HowToCppButton : public QObject +{ + Q_OBJECT + +public: + tst_HowToCppButton(); + +private slots: + void example(); +}; + +tst_HowToCppButton::tst_HowToCppButton() +{ +} + +void tst_HowToCppButton::example() +{ + QTest::failOnWarning(QRegularExpression(QStringLiteral(".?"))); + + QQmlApplicationEngine engine; + engine.loadFromModule("MyModule", "Main"); + QCOMPARE(engine.rootObjects().size(), 1); + + auto *window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0)); + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + auto *button = window->findChild<QQuickButton*>(); + QVERIFY(button); + QCOMPARE(button->text(), "Click me"); + QTest::ignoreMessage(QtDebugMsg, "Did stuff!"); + QVERIFY(clickButton(button)); +} + +QT_END_NAMESPACE + +QTEST_MAIN(tst_HowToCppButton) + +#include "tst_how-to-cpp-button.moc" |