diff --git a/QPainerExample/QPainerExample.pro b/QPainerExample/QPainerExample.pro new file mode 100644 index 0000000..b21a0ce --- /dev/null +++ b/QPainerExample/QPainerExample.pro @@ -0,0 +1,27 @@ +QT += quick + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + qquickcustomitem.cpp + +RESOURCES += qml.qrc + +# Additional import path used to resolve QML modules in Qt Creator's code model +QML_IMPORT_PATH = + +# Additional import path used to resolve QML modules just for Qt Quick Designer +QML_DESIGNER_IMPORT_PATH = + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +HEADERS += \ + qquickcustomitem.h diff --git a/QPainerExample/main.cpp b/QPainerExample/main.cpp new file mode 100644 index 0000000..befb6de --- /dev/null +++ b/QPainerExample/main.cpp @@ -0,0 +1,27 @@ +#include +#include +#include "qquickcustomitem.h" + +int main(int argc, char *argv[]) +{ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +#endif + + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + + qmlRegisterType("stackoverflow.qml", 1, 0, "Triangle"); + + + const QUrl url(QStringLiteral("qrc:/main.qml")); + QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, + &app, [url](QObject *obj, const QUrl &objUrl) { + if (!obj && url == objUrl) + QCoreApplication::exit(-1); + }, Qt::QueuedConnection); + engine.load(url); + + return app.exec(); +} diff --git a/QPainerExample/main.qml b/QPainerExample/main.qml new file mode 100644 index 0000000..5ddd170 --- /dev/null +++ b/QPainerExample/main.qml @@ -0,0 +1,38 @@ +import QtQuick 2.15 +import QtQuick.Window 2.15 +import stackoverflow.qml 1.0 + +Window { + width: 640 + height: 480 + visible: true + title: qsTr("Hello World") + + Rectangle { + width: 200 + height: 200 + anchors.centerIn: parent + color: "lightgrey" + + Triangle { + id: rect + width: 200 + height: 202 +// transformOrigin: Item.Top + color: "green" + onColorChanged: console.log("color was changed"); +// PropertyAnimation on rotation { +// from: 0 +// to: 360 +// duration: 5000 +// loops: Animation.Infinite +// } + } + } + Timer { + interval: 1000 + repeat: true + running: true + onTriggered: rect.color = Qt.rgba(Math.random(),Math.random(),Math.random(),1); + } +} diff --git a/QPainerExample/qml.qrc b/QPainerExample/qml.qrc new file mode 100644 index 0000000..5f6483a --- /dev/null +++ b/QPainerExample/qml.qrc @@ -0,0 +1,5 @@ + + + main.qml + + diff --git a/QPainerExample/qquickcustomitem.cpp b/QPainerExample/qquickcustomitem.cpp new file mode 100644 index 0000000..507533f --- /dev/null +++ b/QPainerExample/qquickcustomitem.cpp @@ -0,0 +1,49 @@ +#include "qquickcustomitem.h" + +QQuickCustomItem::QQuickCustomItem(QQuickPaintedItem *parent) : + QQuickPaintedItem(parent), + m_color(Qt::red), + m_needUpdate(true) +{ + setFlag(QQuickItem::ItemHasContents); +} + +void QQuickCustomItem::paint(QPainter *painter) +{ + + const int scale = 1; + + QPen pen; + pen.setWidth(3); + pen.setDashPattern({ 0.0, 1.0 * scale, 1.0 * scale, 1.0 * scale }); + pen.setColor(m_color); + + QPainterPath path; +// path.moveTo(width() / 2, 0); +// path.lineTo(width(), height()); +// path.lineTo(0, height()); +// path.lineTo(width() / 2, 0); + + qWarning()<window().width()<<" "<window().height(); + path.moveTo(QPoint(100, 0)); + path.arcTo(QRectF(0, 0, painter->window().width(), painter->window().height()), 90, 270); + + + painter->setPen(pen); + painter->drawPath(path); +} + +QColor QQuickCustomItem::color() const +{ + return m_color; +} + +void QQuickCustomItem::setColor(const QColor &color) +{ + if(m_color != color) { + m_color = color; + m_needUpdate = true; + update(); + emit colorChanged(); + } +} diff --git a/QPainerExample/qquickcustomitem.h b/QPainerExample/qquickcustomitem.h new file mode 100644 index 0000000..9482ef2 --- /dev/null +++ b/QPainerExample/qquickcustomitem.h @@ -0,0 +1,29 @@ +#ifndef QQUICKCUSTOMITEM_H +#define QQUICKCUSTOMITEM_H + +#include +#include +#include + +class QQuickCustomItem : public QQuickPaintedItem +{ + Q_OBJECT + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) +public: + QQuickCustomItem(QQuickPaintedItem *parent = Q_NULLPTR); + +protected: + void paint(QPainter *painter); + + QColor color() const; + void setColor(const QColor &color); + +private: + QColor m_color; + bool m_needUpdate; + +signals: + void colorChanged(); +}; + +#endif // QQUICKCUSTOMITEM_H