有什麼方法可以在QML中更改光標形狀嗎?我使用的是Qt 4.7和Python,所以我不能使用Qt Quick 2,而在Qt Quick中沒有光標形狀選項。QML中的光標形狀
1
A
回答
4
是的,有一種方法,雖然不是裏面我所知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光標形狀像這樣(沒有自定義光標,不過,你可以嘗試改變這個神奇位),添加這些行的main.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到矩形例如:
CursorShapeArea {
anchors.fill: parent
anchors.margins: 50
cursorShape: Qt.OpenHandCursor
}
0
一個例子:
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:
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]
}
}
相關問題
- 1. 改變光標形狀有複製光標形狀
- 2. Javafx - 光標相交形狀
- 3. QML:自定義光標
- 4. 即使在QML中的重疊矩形上光標形狀也發生了改變
- 5. 在光標位置插入Word形狀
- 6. Word VSTO - 在光標點插入形狀?
- 7. urxvt光標形狀設置爲|
- 8. 懸停vim設置光標形狀
- 9. 如何更改gWidgets RGtk2中的形狀鼠標光標?
- 10. 如何改變光標形狀在QML當鼠標區域覆蓋有另一鼠標區域
- 11. 如何根據Bash中的VI模式更改光標形狀?
- 12. 如何更改整個畫布的鼠標光標形狀?
- 13. Graphics.fill(形狀)不光柵
- 14. 如何在QTextEdit中將光標形狀設置爲'>'?
- 15. 如何更改Windows從C++中閃爍光標形狀?
- 16. vim中的柱狀光標運動?
- 17. Ymacs的條形光標
- 18. 在QML樹狀
- 19. 如何辨別光標的當前形狀?
- 20. 如何更改QWS上的光標形狀?
- 21. 如何更改QPlainTextEdit(Pyqt,PySide)的光標形狀
- 22. Objective C iPhone編程點或形狀來跟隨你的光標
- 23. 如何替換QML程序中的所有光標外觀?
- 24. Qt QML - 在Qt中創建我自己的形狀
- 25. QML Canvas中的SVG光柵化問題
- 26. 圖形程序集中的光標
- 27. XCode中的矩形光標鍵盤
- 28. 如果光標在光標上,則顯示矩形,如果光標在光標上,則顯示矩形;如果光標離開子窗口,則顯示矩形
- 29. JDBC - 無效光標狀態
- 30. VB6標籤集的手形光標
@Downvoter請不要downvote沒有在某人的第一個問題的解釋 - 是不鼓勵他們回來。 – cmannett85
我已經明白了一切,別擔心 –