我尝试将Qml和OpenGL联系起来,所以我举了例子openglunderqml
在示例中,我创建自己的类
class QProject1Main : public QQuickItem
void QProject1Main::handleWindowChanged(QQuickWindow *win) {
if (win) {
connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
win->setClearBeforeRendering(false);
}
}
void QProject1Main::paint() {
qDebug() << "QProject1Main::paint()";
qreal ratio = window()->devicePixelRatio();
int w = int(ratio * window()->width());
int h = int(ratio * window()->height());
glViewport(0, 0, w, h);
// each iteration bg color change from green to read
glClearColor(((float)++idx)/100, 0.3, 0.3, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// triangle visible only at first iteration
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.1f, 0.8f, 0.1f);
glVertex3f(-0.8f,-0.8f, 0.1f);
glVertex3f( 0.8f,-0.8f, 0.1f);
glEnd();
}
我调整窗口大小来调用paint方法,看到背景颜色发生了变化但是第一次只涂了三角形(