这应该是如何将自定义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);
}发布于 2014-05-31 14:31:09
您的代码有两个问题。首先,ApplicationWindow没有颜色属性,然后当您设置
color: "red"在这个组件中,您不设置任何颜色(即颜色是黑色)。您可以为ApplicationWindow设置背景颜色,在GLViewer之前添加一个矩形组件,如下所示
Rectangle {
width: parent.width
height: parent.height
anchors.centerIn: parent
color: "red"
}第二,您是在主windows GL上下文中绘制的,即使viewport设置正确,也可以使用以下代码行
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);会清除整扇窗户。如果只想清除窗口的一部分,则必须使用glScissor。
glViewport(x, y, w, h);
glEnable(GL_SCISSOR_TEST);
glScissor(x,y,w,h);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);您可以在github上找到完整的示例(基于您的链接)。
https://stackoverflow.com/questions/23968598
复制相似问题