这应该是将自定义OpenGL添加到QML应用程序中的最佳方法。
http://qt-project.org/doc/qt-5/qtquick-scenegraph-openglunderqml-example.html
问题在于,我不想覆盖整个窗口,而只想在我的OpenGL自定义Qt快速项所占用的矩形中绘制。我认为可以使用适当的参数调用glViewport,因此OpenGL将只绘制我的项的矩形。
但实际上,这对我不起作用。
QML:
import QtQuick 2.2
import QtQuick.Controls 1.1
import ge.components 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
color: "red"
menuBar: MenuBar {
Menu {
title: qsTr("Filxe")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
GLViewer {
width: 200
height: 200
x: 100
y: 100
}
}
QT Quick项: 在绘制方法中,我首先使用ApplicationWindow的颜色填充整个窗口,然后我想用黑色填充仅由我的项占据的矩形。实际上它会用黑色填满整个窗口,为什么???
#include "glviewer.h"
#include <QQuickWindow>
#include <iostream>
#include <QColor>
using namespace std;
GLViewer::GLViewer(QQuickItem *parent) :
QQuickItem(parent)
{
connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, SLOT(handleWindowChanged(QQuickWindow*)));
}
void GLViewer::handleWindowChanged(QQuickWindow *win)
{
if (win) {
connect(win, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
win->setClearBeforeRendering(false);
}
}
void GLViewer::paint() {
QColor color = window()->color();
glClearColor(color.red(), color.green(), color.blue(), color.alpha());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cout << "X: " << x() << ", Y: " << y() << ", W: " << width() << ", H: " << height() << endl;
glViewport(x(), y(), width(), height());
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
ApplicationWindow
继承Window
,Window
拥有color
属性。 - Dmitry Sokolov