我試圖在QML component which embeds one C++ class上設計應用程序。 我的階級是這樣的:如何在QML組件中使用C++類
class Fleche : public QQuickPaintedItem // Fleche = arrow
{
public :
Fleche();
~Fleche();
protected :
QPainterPath arrow;
private :
void paint(QPainter *painter){
// Draw
arrow.moveTo(50,50);
arrow.lineTo(0,0);
arrow.cubicTo(20,100/3,20,2*100/3,0,100);
arrow.closeSubpath();
// Paint
QLinearGradient gradient(0, 0, 0, 100);
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(1.0, Qt::darkBlue);
QBrush fill(gradient);
painter->setBrush(fill);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawPath(arrow);
}
};
此產品應該在我的QML代碼實現4倍,並且需要進行旋轉,動畫和調整大小。 我想我必須使用updatePaintNode(),但作爲一個設計師,(至少不是一個編碼器),我有麻煩混合語言...... 的想法是在我的QML代碼:
Fleche {
id : arrowNumberX // X is from 0 to 3
width : 0.1 * parent.width
height : 0.15 * parent.height
z : 1
rotation : X * 90
MouseArea{ /.../}
SequentialAnimation{/.../}
}
任何例子或者幫助關於如何在我的QML應用程序中實現我的對象將會幫助我很多!
我有兩個問題: 1)爲什麼QQuickItem類比QQuickPaintedItem類更快?最後一個不是第一個孩子嗎? 2)我打算使用C++代碼,但是如何在我的QML代碼中使用它? Ps:我是這個論壇的新手我不知道我是否應該在評論部分提問,對不起,如果我不應該! – TaiZzZ
第一種方式使用OpenGL,而第二種方式使用QPainter即。首先使用QPainter繪製項目,然後將此圖像繪製爲場景圖形中的OpenGL紋理。示例可以在[這裏]找到(https://stackoverflow.com/documentation/qml/6509/creating-custom-elements-in-c/22330/creating-custom-elements-in-c#t=201706131403549080664) – folibis