Skip to content

Instantly share code, notes, and snippets.

@Soulstorm50
Created August 10, 2017 13:04
Show Gist options
  • Save Soulstorm50/72b64c4e602581f41783e48103b8b238 to your computer and use it in GitHub Desktop.
Save Soulstorm50/72b64c4e602581f41783e48103b8b238 to your computer and use it in GitHub Desktop.
circles on screen
#include "circle.h"
Circle::Circle(QObject *parent) : QObject(parent)
,x(rand() % 600)
,y(rand() % 600)
{
}
int Circle::getY()
{
y += 1;
return y;
}
int Circle::getX()
{
x +=1;
return x;
}
Circle::slotPositionX()
{
emit getX();
}
Circle::slotPositionY()
{
emit getY();
}
#ifndef CIRCLE_H
#define CIRCLE_H
#include <QObject>
class Circle : public QObject
{
Q_OBJECT
Q_PROPERTY(int x READ getX NOTIFY xPosition())
Q_PROPERTY(int y READ getY NOTIFY yPosition())
public:
explicit Circle(QObject *parent = nullptr);
int getX();
int getY();
signals:
void xPosition();
void yPosition();
public slots:
slotPositionX();
slotPositionY();
private:
int x;
int y;
};
#endif // CIRCLE_H
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QObject>
#include "circle.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
qmlRegisterType<Circle>("circle", 1, 0, "Circle");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
import QtQuick 2.6
import QtQuick.Window 2.2
import circle 1.0
Window {
visible: true
width: 800
height: 600
title: qsTr("Test")
ListView {
id: listView
width: 600
height: 600
delegate:
Rectangle {
id: circle
width: 60
height: 60
color: "black"
radius: 40
x: 0
y: 0
Circle{
id: objCircle
}
Behavior on x {
SpringAnimation{
spring: 2
damping: 0.2
}
}
Behavior on y {
SpringAnimation{
spring: 2
damping: 0.2
}
}
MouseArea{
anchors.fill: parent
onClicked: {
if(circle.state == 1)
{
circle.visible = false
}
else
{
circle.color = "red"
circle.state = 1;
}
}
}
Timer {
interval: 1; running: true; repeat: true
onTriggered: {
x = objCircle.slotPositionX()
y = objCircle.slotPositionY()
}
}
}
model: ListModel {
id: listModel
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
ListElement{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment