parent
718bb24889
commit
c9c65af940
@ -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
|
@ -0,0 +1,27 @@
|
|||||||
|
#include <QGuiApplication>
|
||||||
|
#include <QQmlApplicationEngine>
|
||||||
|
#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<QQuickCustomItem>("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();
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
<RCC>
|
||||||
|
<qresource prefix="/">
|
||||||
|
<file>main.qml</file>
|
||||||
|
</qresource>
|
||||||
|
</RCC>
|
@ -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()<<painter->window().width()<<" "<<painter->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();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef QQUICKCUSTOMITEM_H
|
||||||
|
#define QQUICKCUSTOMITEM_H
|
||||||
|
|
||||||
|
#include <QQuickPaintedItem>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QPainterPath>
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in new issue