本文参照官方openglunderqml改写,基本框架不变,若用opengl3.3 只需在main函数中设置format即可,如下
<span style="font-family:Courier New;"> QSurfaceFormat format;
format.setVersion(3,3);
format.setProfile(QSurfaceFormat::CoreProfile);
qmlRegisterType<RenderWindow>("OpenGLUnderQML", 1, 0, "RenderWindow");
view1.setFormat(format);</span>
还有个问题是官方在此例中并没有提供鼠标交互功能,在本例中简单实现了下。在
openglunderqml这个例子中如果添加item继承而来的mouseEvent事件,并不起作用,当在QML中在该注册类里面添加mousearea也不起作用,所以,只能把鼠标作用区放在全局下面,代码如下:
import QtQuick 2.0
import QtQuick 2.0
import OpenGLUnderQML 1.0
import QtQuick.Controls 1.2
Item {
width: 300
height: 400
RenderWindow {
id:rw
width:200
height:100
SequentialAnimation on t {
NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
loops: Animation.Infinite
running: true
}
}
MouseArea
{
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
onPressed: { rw.point = Qt.vector2d(mouseX,mouseY); }
onPositionChanged:
{
rw.nPoint = Qt.vector2d(mouseX,mouseY);
rw.point = Qt.vector2d(mouseX,mouseY);
}
}
//! [1] //! [2]
Rectangle {
color: Qt.rgba(1, 1, 1, 0.7)
radius: 10
border.width: 1
border.color: "white"
anchors.fill: label
anchors.margins: -10
}
Text {
id: label
color: "black"
wrapMode: Text.WordWrap
text: "lable"
anchors.right: parent.right
anchors.left: parent.left
anchors.bottom: parent.bottom
anchors.margins: 20
}
Button{
id:button1
text: "set pos"
x:50
y:60
// activeFocusOnPress : bool
onClicked: {
label.text = "ds" ;
}
}
}
源码在此