Clean up the painteditem example

The way it was before it was unusable. Move the files that belong to the
TextBalloon module into their own directory, and add a wrapper
application that loads textballoons.qml with the right parameters to
actually find the TextBalloon module. Make sure that the qmldir file is
copied to the output directory.

Pick-to: 6.2
Change-Id: Ie7407a425a0a95a46de6486f9a28fd1aee07de9f
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2021-08-31 14:59:05 +02:00
parent 485416c9d5
commit 79cae5f652
14 changed files with 150 additions and 83 deletions

View File

@ -1,62 +1,37 @@
# Generated from painteditem.pro.
cmake_minimum_required(VERSION 3.16)
project(qmltextballoonplugin LANGUAGES CXX)
project(painteditem LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(NOT DEFINED INSTALL_EXAMPLESDIR)
set(INSTALL_EXAMPLESDIR "examples")
endif()
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/customitems/painteditem/TextBalloonPlugin")
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples/quick/customitems/painteditem)
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/customitems/painteditem")
find_package(Qt6 COMPONENTS Core)
find_package(Qt6 COMPONENTS Gui)
find_package(Qt6 COMPONENTS Qml)
find_package(Qt6 COMPONENTS Quick)
find_package(Qt6 COMPONENTS Core Gui Quick Qml)
qt6_add_qml_module(qmltextballoonplugin
add_subdirectory(TextBalloon)
qt_add_executable(painteditemexample WIN32 MACOSX_BUNDLE main.cpp)
qt_add_qml_module(painteditemexample
URI painteditem
VERSION 1.0
URI "TextBalloonPlugin"
PLUGIN_TARGET qmltextballoonplugin
QML_FILES textballoons.qml
RESOURCE_PREFIX "/painteditem"
QML_FILES
"textballoons.qml"
)
target_sources(qmltextballoonplugin PRIVATE
textballoon.cpp textballoon.h
)
set_target_properties(qmltextballoonplugin PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_link_libraries(qmltextballoonplugin PUBLIC
target_link_libraries(painteditemexample PRIVATE
Qt::Core
Qt::Gui
Qt::Qml
Qt::Quick
qmltextballoon
)
# Resources:
set(painteditem_resource_files
"textballoon.h"
)
qt6_add_resources(qmltextballoonplugin "painteditem"
PREFIX
"/painteditem"
FILES
${painteditem_resource_files}
)
install(TARGETS qmltextballoonplugin
install(TARGETS painteditemexample
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"

View File

@ -0,0 +1,27 @@
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/quick/customitems/painteditem/TextBalloon")
qt_add_library(qmltextballoon)
qt_add_qml_module(qmltextballoon
VERSION 1.0
URI "TextBalloon"
PLUGIN_TARGET qmltextballoon
SOURCES
textballoon.cpp textballoon.h
)
target_link_libraries(qmltextballoon PUBLIC
Qt::Core
Qt::Gui
Qt::Qml
Qt::Quick
)
install(TARGETS qmltextballoon
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/qmldir
DESTINATION "${INSTALL_EXAMPLEDIR}"
)

View File

@ -50,7 +50,7 @@
#include <QQmlEngineExtensionPlugin>
#include "../textballoon.h"
#include "textballoon.h"
class TextBalloonPlugin : public QQmlEngineExtensionPlugin
{

View File

@ -0,0 +1,28 @@
TEMPLATE = lib
CONFIG += plugin qmltypes
QT += qml quick
QML_IMPORT_NAME = TextBalloon
QML_IMPORT_MAJOR_VERSION = 1
TARGET = qmltextballoonplugin
HEADERS += \
plugin.h \
textballoon.h
SOURCES += textballoon.cpp
RESOURCES += textballoon.qrc
qmldir_output.files = qmldir
qmldir_output.path = $$OUT_PWD
COPIES += qmldir_output
target.path = $$[QT_INSTALL_EXAMPLES]/quick/customitems/painteditem/$$QML_IMPORT_NAME
qmldir_install.files = qmldir
qmldir_install.path = $$[QT_INSTALL_EXAMPLES]/quick/customitems/painteditem/$$QML_IMPORT_NAME
INSTALLS += qmldir_install target
CONFIG += install_ok # Do not cargo-cult this!

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/TextBalloon">
<file>qmldir</file>
</qresource>
</RCC>

View File

@ -38,16 +38,14 @@
The QQuickPaintedItem class is a class derived from QQuickItem for implementing
custom QML Scene Graph items using the QPainter interfaces.
The example consists of an item class, a plugin class and a QML file
to use this plugin. The \c TextBalloon class represents the individual
text balloons extending QQuickPaintedItem, the \c TextBalloonPlugin class
represents the skeleton code for a \l {Qt Quick} plugin and the
\c textballoons.qml file is used to load the plugin and display the text
balloons.
The example consists of an item class and a QML file to use the item. The
\c TextBalloon class represents the individual text balloons extending
QQuickPaintedItem and the \c textballoons.qml file is used to load the module
containing the TextBalloon QML type and display the text balloons.
We will focus on the \c TextBalloon class first and continue with the
\c textballoons.qml file. For an example on how to implement a \l {Qt Quick}
plugin please look at \l{Chapter 6: Writing an Extension Plugin}
\c textballoons.qml file. For an example on how to implement a plugin for a
QML module please look at \l{Chapter 6: Writing an Extension Plugin}
{Writing an Extension Plugin}
\section1 TextBalloon Class Declaration
@ -56,7 +54,7 @@
is the base class for all QPainter based items in the QML Scene Graph
framework.
\snippet customitems/painteditem/textballoon.h 0
\snippet customitems/painteditem/TextBalloon/textballoon.h 0
To implement a QQuickPaintedItem you must implement QQuickPaintedIem's pure
virtual function \l {QQuickPaintedItem::}{paint()} which implements the
@ -67,13 +65,13 @@
We have to be sure to initialize the rightAligned property for a
TextBalloon item.
\snippet customitems/painteditem/textballoon.cpp 0
\snippet customitems/painteditem/TextBalloon/textballoon.cpp 0
Then we implement the \c paint() function which is automatically called by
the Scene Graph framework to paint the contents of the item. The function
paints the item in local coordinates.
\snippet customitems/painteditem/textballoon.cpp 1
\snippet customitems/painteditem/TextBalloon/textballoon.cpp 1
We start with setting the pen and brush on the item to define the look of
the item. After that we start drawing. Note that the \l {QQuickPaintedItem::}{contentsBoundingRect()}
@ -81,7 +79,7 @@
returned by the \l {QQuickPaintedItem::}{contentsBoundingRect()} function is the size
of the item as defined in the QML file.
\section1 Textballoons.qml File
\section1 textballoons.qml File
The Interface consists of two main parts. The scrollable area with the
textballoons and the controls button to add new balloons.

View File

@ -0,0 +1,51 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, 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 "../../shared/shared.h"
DECLARATIVE_EXAMPLE_MAIN(painteditem/textballoons)

View File

@ -1,29 +1,2 @@
TEMPLATE = lib
CONFIG += plugin qmltypes
QT += qml quick
QML_IMPORT_NAME = TextBalloonPlugin
QML_IMPORT_MAJOR_VERSION = 1
TARGET = qmltextballoonplugin
HEADERS += \
TextBalloonPlugin/plugin.h \
textballoon.h
SOURCES += textballoon.cpp
RESOURCES += painteditem.qrc
DESTDIR = $$QML_IMPORT_NAME
target.path = $$[QT_INSTALL_EXAMPLES]/quick/customitems/painteditem/$$QML_IMPORT_NAME
qmldir.files = $$QML_IMPORT_NAME/qmldir
qmldir.path = $$[QT_INSTALL_EXAMPLES]/quick/customitems/painteditem/$$QML_IMPORT_NAME
INSTALLS += qmldir target
CONFIG += install_ok # Do not cargo-cult this!
OTHER_FILES += \
textballoons.qml
TEMPLATE = subdirs
SUBDIRS = TextBalloon/textballoon.pro textballoonuser.pro

View File

@ -1,6 +1,5 @@
<RCC>
<qresource prefix="/painteditem">
<file>textballoon.h</file>
<file>textballoons.qml</file>
</qresource>
</RCC>

View File

@ -49,7 +49,7 @@
****************************************************************************/
import QtQuick 2.0
import "TextBalloonPlugin" 1.0
import TextBalloon 1.0
Item {
height: 480

View File

@ -0,0 +1,10 @@
TEMPLATE = app
QT += qml quick
TARGET = painteditem
SOURCES += main.cpp
RESOURCES += painteditem.qrc
target.path = $$[QT_INSTALL_EXAMPLES]/quick/customitems/painteditem
INSTALLS += target