Skip to content
Snippets Groups Projects
Commit b628c798 authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

Add custom QQuickItem that changes cursor

parent 182b3f45
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(quick-custom-cursor
icons.qrc
main.cpp
myitem.cpp
myitem.h
qml.qrc
)
......
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include "myitem.h"
int main(int argc, char *argv[])
{
......@@ -7,6 +9,8 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
qmlRegisterType<MyItem>("MyTypes", 1, 0, "MyItem");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
......
import QtQuick 2.12
import QtQuick.Window 2.12
import MyTypes 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Column {
anchors.fill: parent
Item {
anchors.left: parent.left
anchors.right: parent.right
height: parent.height / 3
Rectangle {
anchors.fill: parent
color: "#ccc"
}
Text {
anchors.centerIn: parent
text: "default"
}
}
MyItem {
anchors.left: parent.left
anchors.right: parent.right
height: parent.height / 3
cursorSource: "icons/emacs.png"
Rectangle {
anchors.fill: parent
color: "#eee"
}
Text {
anchors.centerIn: parent
text: "Emacs"
}
}
MyItem {
anchors.left: parent.left
anchors.right: parent.right
height: parent.height / 3
cursorSource: "icons/gnome.png"
Rectangle {
anchors.fill: parent
color: "#ccc"
}
Text {
anchors.centerIn: parent
text: "Gnome"
}
}
}
}
#include <QCursor>
#include <QPixmap>
#include <QQmlFile>
#include "myitem.h"
MyItem::MyItem(QQuickItem *parent)
: QQuickItem(parent)
{
}
QUrl MyItem::cursorSource() const
{
return cursorSource_;
}
void MyItem::setCursorSource(const QUrl &source)
{
if (cursorSource_ == source)
return;
QPixmap pix(QQmlFile::urlToLocalFileOrQrc(source));
setCursor(QCursor(pix));
emit cursorSourceChanged(source);
}
myitem.h 0 → 100644
#ifndef MYITEM_H
#define MYITEM_H
#include <QQuickItem>
#include <QUrl>
class MyItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QUrl cursorSource READ cursorSource WRITE setCursorSource NOTIFY cursorSourceChanged)
public:
explicit MyItem(QQuickItem *parent = nullptr);
QUrl cursorSource() const;
void setCursorSource(const QUrl &source);
signals:
void cursorSourceChanged(const QUrl &source);
private:
QUrl cursorSource_;
};
#endif // MYITEM_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment