Created
July 4, 2016 13:55
-
-
Save mitchcurtis/4792dfaa46b8a40b9ffcbc0e33b20283 to your computer and use it in GitHub Desktop.
Custom QQuickPaintedItem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGuiApplication> | |
#include <QQmlApplicationEngine> | |
#include <QQmlContext> | |
#include <QQuickPaintedItem> | |
#include <QPainter> | |
class PaintedItem : public QQuickPaintedItem | |
{ | |
Q_OBJECT | |
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) | |
public: | |
PaintedItem() { | |
} | |
void paint(QPainter *painter) override { | |
painter->fillRect(0, 0, width(), height(), mColor); | |
} | |
QColor color() const { | |
return mColor; | |
} | |
void setColor(const QColor &color) { | |
if (color == mColor) | |
return; | |
mColor = color; | |
update(); | |
emit colorChanged(); | |
} | |
signals: | |
void colorChanged(); | |
private: | |
QColor mColor; | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QGuiApplication app(argc, argv); | |
qmlRegisterType<PaintedItem>("App", 1, 0, "PaintedItem"); | |
QQmlApplicationEngine engine; | |
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); | |
return app.exec(); | |
} | |
#include "main.moc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.7 | |
import QtQuick.Window 2.0 | |
import QtQuick.Controls 2.1 | |
import App 1.0 | |
ApplicationWindow { | |
id: mainWindow | |
x: Screen.width / 2 - width / 2 | |
y: Screen.height / 2 - height / 2 | |
width: 400 | |
height: 200 | |
visible: true | |
Shortcut { | |
sequence: "Ctrl+Q" | |
onActivated: Qt.quit() | |
} | |
PaintedItem { | |
width: 100 | |
height: 100 | |
color: "red" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment