python - QML 中的光标形状

标签 python qt qml

有什么方法可以改变 QML 中的光标形状吗?我正在使用 Qt 4.7 和 Python,所以我不能使用 Qt Quick 2,而且在 Qt Quick 中没有光标形状选项。

最佳答案

是的,虽然我知道有一种方法不是在 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));

或者,您可以将光标更改为类似这样的 qt 光标形状(但不是自定义光标,您可以尝试更改此魔术位),将这些行添加到 ma​​in.cpp

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

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:

#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

在您的 QML 文件中:

import Cursor 1.0

并将 CursorShapeArea 添加到 Rectangle 例如:

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

关于python - QML 中的光标形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17210235/

相关文章:

python - 在 Python 中将多个字符串写入 CSV 文件

从单独的线程运行的Python ServerSocket

python Bokeh 单元测试

c++ - Qt 接口(interface)类

c++ - 使用系统 DBus 的 registerService 失败(但适用于 session DBus)

Qt - 模块 "QtQuick.Controls"未安装

c++ - QML:如何将着色器效果与 QtGraphical 效果混合?

python - UDP 连接没有收到来自服务器的任何回复 - Python(也可能使用 boost 的 C++)

qt - 在 ubuntu 22.04 上启动 spyder 时出错

c++ - 当底层模型更改时,QT QmlMap PolyLine 未正确更新