1 Attachment(s)
Render OpenGL graphics in a child window
Hi there,
I followed an online guide and can render OpenGL image in a QML QQuickItem now.
As shown in the picture:
Attachment 13823
The black wireframe in left is defined as QQuickItem area (OpenGL rendering area, 800 * 600 px), but we can see, the rendered OpenGL result shows in the whole background.
In my software, I'll have a few components shown on the screen. OpenGL render area should be in one of the components. Say, stay in right-lower corner of the screen.
Can I render OpenGL in a child window? And can I place the child window in the place I want?
Any suggestions for me?
And have I made it clear for you?
I tried to create a new QQuickWindow. But it shows as a stand alone window not as a child window or a part of the background.
Thank you.
Re: Render OpenGL graphics in a child window
Hi, can you show us your code?
Ginsengelf
Re: Render OpenGL graphics in a child window
Thank you, Ginsengelf.
The code is quite scattered, I'll try to Jigsaw it out.
OpenGLItem in the "Main.qml" piece is the QQuickItem to render OpenGL scene:
Quote:
OpenGLItem {
id: openGLItem
visible: true
anchors.top: tab1.bottom
width: 800
height: 600
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "black"
border.width: 1
}
}
This is a piece of "main.cpp":
Quote:
int main(int argc, char *argv[])
{
// QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);
QGuiApplication app(argc, argv);
// For OpenGL rendering
QQuickWindow::setGraphicsApi(QSGRendererInterface: :OpenGL);
// Register OpenGL rendering QQuickItem
qmlRegisterType<OpenGLItem>("com.OpenGLItem", 1, 0, "OpenGLItem");
// Register Object receiving function calls from QML to C++
qmlRegisterType<Tab1Panel>("com.tab1panel", 1, 0, "Tab1Panel");
QQmlApplicationEngine engine;
const QUrl url(u"qrc:/Slicer3D/Main.qml"_qs);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.load(url);
Here "render()" is to render OpenGL scene, "m_window" is from "window()" method call of "OpenGLItem", the QQuickItem:
Quote:
void OGLRender::paint()
{
m_window->beginExternalCommands();
render();
m_window->endExternalCommands();
}
I wrote the code referencing this tutorial:
https://doc.qt.io/qt-6/qtquick-scene...l-example.html
As we can see, in the code, we don't have a chance to assign any window handle to OpenGL.
I think OpenGL just is rendered on the whole background of application window.
My current solution is to render OpenGL scene to an offscreen framebuffer,
And draw the rendered buffer with 2 triangles on to part of the application window background.
Say, We set top-left vertex as -1.0, 1.0,
bottom-right vertex as 0.0, 0.0,
The OpenGL scene would be rendered in top-left quarter of the screen.
But resizing the window makes the OpenGL scene showing deformed.
My solution is to fix the application window as maximized size.
That's my current solution, not easy and not quite good, I have to say.