简体   繁体   English

QML中的光标形状

[英]Cursor shape in QML

Is there any way to change the cursor shape in QML? 有没有办法在QML中更改光标形状? I am using Qt 4.7 and Python, so I can't use Qt Quick 2, and in Qt Quick there in no cursor shape option. 我使用Qt 4.7和Python,所以我不能使用Qt Quick 2,而在Qt Quick中没有光标形状选项。

Yes, there is a way, though, not inside of QML as i know, but in c++ part of the program, example of main.cpp file: 是的,有一种方法,但不是我所知道的QML内部,但在c ++程序的一部分,main.cpp文件的例子:

QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/main.qml"));
viewer.showExpanded();

//changing cursor
viewer.setCursor(QPixmap(":/peach.png").scaledToWidth(20));

or, you can change cursor to qt cursor shapes something like this (not custom cursors, though, you can try to change this magic bit), add these lines to main.cpp 或者,您可以将光标更改为qt光标形状(不是自定义光标,但您可以尝试更改此魔术位),将这些行添加到main.cpp

#include "cursorshapearea.h"
qmlRegisterType<QsltCursorShapeArea>("Cursor", 1, 0, "CursorShapeArea");

cursorshapearea.cpp : cursorshapearea.cpp

#include "cursorshapearea.h"

QsltCursorShapeArea::QsltCursorShapeArea(QDeclarativeItem *parent) :
  QDeclarativeItem(parent),
  m_currentShape(-1)
{
}

Qt::CursorShape QsltCursorShapeArea::cursorShape() const
{
  return cursor().shape();
}

void QsltCursorShapeArea::setCursorShape(Qt::CursorShape cursorShape)
{
  if (m_currentShape == (int) cursorShape)
    return;

  setCursor(cursorShape);
  emit cursorShapeChanged();

  m_currentShape = cursorShape;
}

cursorshapearea.h : cursorshapearea.h

#ifndef CURSORSHAPEAREA_H
#define CURSORSHAPEAREA_H

#include <QDeclarativeItem>

class QsltCursorShapeArea : public QDeclarativeItem
{
  Q_OBJECT

  Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape NOTIFY cursorShapeChanged)

public:

  explicit QsltCursorShapeArea(QDeclarativeItem *parent = 0);

  Qt::CursorShape cursorShape() const;
  Q_INVOKABLE void setCursorShape(Qt::CursorShape cursorShape);

private:
  int m_currentShape;

signals:
  void cursorShapeChanged();
};

#endif // CURSORSHAPEAREA_H

and in your QML file: 在您的QML文件中:

import Cursor 1.0

and add CursorShapeArea to Rectangle for example: 并将CursorShapeArea添加到Rectangle例如:

CursorShapeArea {
    anchors.fill: parent
    anchors.margins: 50
    cursorShape: Qt.OpenHandCursor
  }

An example: 一个例子:

main.py: main.py:

import sys
from PySide.QtCore import *    
from PySide.QtGui import *
from PySide.QtDeclarative import *



class CursorArea (QDeclarativeItem):

    def __init__(self, parent = None):
        QDeclarativeItem.__init__(self, parent)
        self._cursors = Qt.CursorShape.ArrowCursor
        self._cursorsPix = "None"
        self._cursorsPixUrl = []

        self.dictPix = {"lapizAzul": QCursor(QPixmap("lapiz1.png"),1,32),
                        "lapizVerde": QCursor(QPixmap("lapiz1.png"),1,32),
                        "lapizCorona": QCursor(QPixmap("lapiz1.png"),1,32),
                        }


    def getCursors(self):
        return self_cursors

    def setCursors(self,value):
        self._cursors = value
        self.setCursor(value)
    cursors = Property(Qt.CursorShape, getCursors, setCursors)

    def getCursorsPix(self):
        return self._cursorsPix

    def setCursorsPix(self, value):
        print (value)
        pixmap = self.buscarPixmap(value)
        self.setCursor(pixmap)
    cursorsPix = Property("QString", getCursorsPix, setCursorsPix)

    def buscarPixmap(self, pix):
        if (pix in self.dictPix) == True:
            pixmap = self.dictPix[pix]
        else:
            pixmap = Qt.CursorShape.WhatsThisCursor
        return pixmap

    def getCursorsPixUrl(self):
        return self._cursorsPixUrl

    def setCursorsPixUrl(self, lista):
        print (lista)

        self.setCursor(QCursor(QPixmap(lista[0]),lista[1],lista[2]))
    cursorsPixUrl = Property("QVariantList", getCursorsPixUrl, setCursorsPixUrl)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    qmlRegisterType(CursorArea, "Charts", 1, 0, "CursorArea");

    view = QDeclarativeView()
    view.setSource(QUrl.fromLocalFile('app.qml'))
    view.show()
    sys.exit(app.exec_())

app.qml: app.qml:

import Charts 1.0
import QtQuick 1.0

Item {
    width: 300; height: 200

    CursorArea{

        id:ca
        anchors.fill: parent

        //cursors:Qt.PointingHandCursor
        //cursorsPix: "lapizAzul"

        cursorsPixUrl: ["cursorEstrella.png",1,32]
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM