【发布时间】:2014-07-21 00:57:38
【问题描述】:
这应该是如何将自定义 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 快速项目: 在绘画方法中,我首先用 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);
}
【问题讨论】: