Skip to content

Commit 8305ab4

Browse files
committed
Fix setting Qt::AA_UseDesktopOpenGL which should be set before QApplication creation
1 parent bf81b9c commit 8305ab4

5 files changed

Lines changed: 63 additions & 64 deletions

File tree

occt-qopenglwidget/main.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <QSlider>
1919
#include <Standard_WarningsRestore.hxx>
2020

21-
#include <OSD_Environment.hxx>
2221
#include <Standard_Version.hxx>
2322

2423
//! Main application window.
@@ -169,36 +168,15 @@ class MyMainWindow : public QMainWindow
169168

170169
int main(int theNbArgs, char** theArgVec)
171170
{
172-
#if defined(_WIN32)
173-
//
174-
#elif defined(__APPLE__)
175-
//
176-
#else
177-
// Qt6 tries to use Wayland platform by default, which is incompatible with OCCT depending on Xlib;
178-
// Force 'xcb' platform plugin (alternatively, could be passed QApplication as '-platfom xcb' argument).
179-
OSD_Environment aQpaPlat("QT_QPA_PLATFORM");
180-
if (aQpaPlat.Value().IsEmpty())
181-
{
182-
aQpaPlat.SetValue("xcb");
183-
aQpaPlat.Build();
184-
}
185-
#endif
186-
171+
// before creaing QApplication: define platform plugin to load (e.g. xcb on Linux)
172+
// and graphic driver (e.g. desktop OpenGL with desired profile/surface)
173+
OcctQtTools::qtGlPlatformSetup();
187174
QApplication aQApp(theNbArgs, theArgVec);
188175

189176
QCoreApplication::setApplicationName("OCCT Qt/QOpenGLWidget Viewer sample");
190177
QCoreApplication::setOrganizationName("OpenCASCADE");
191178
QCoreApplication::setApplicationVersion(OCC_VERSION_STRING_EXT);
192179

193-
// OpenGL setup managed by Qt
194-
#if defined(_WIN32)
195-
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this
196-
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
197-
// use Qt::AA_UseOpenGLES for embedded systems
198-
#endif
199-
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
200-
QSurfaceFormat::setDefaultFormat(aGlFormat);
201-
202180
MyMainWindow aMainWindow;
203181
aMainWindow.resize(aMainWindow.sizeHint());
204182
aMainWindow.show();

occt-qt-tools/OcctQtTools.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
#include "OcctQtTools.h"
44

55
#include <OpenGl_Caps.hxx>
6+
#include <OSD_Environment.hxx>
67
#include <Standard_Version.hxx>
78

9+
#include <QCoreApplication>
10+
811
// ================================================================
912
// Function : qtColorToOcct
1013
// ================================================================
@@ -22,6 +25,52 @@ QColor OcctQtTools::qtColorFromOcct(const Quantity_Color& theColor)
2225
return QColor((int)Round(anRgb.r() * 255.0), (int)Round(anRgb.g() * 255.0), (int)Round(anRgb.b() * 255.0));
2326
}
2427

28+
// ================================================================
29+
// Function : qtGlPlatformSetup
30+
// ================================================================
31+
void OcctQtTools::qtGlPlatformSetup()
32+
{
33+
#if defined(_WIN32)
34+
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this;
35+
// use Qt::AA_UseOpenGLES for embedded systems
36+
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
37+
#elif defined(__APPLE__)
38+
//
39+
#else
40+
// Qt6 tries to use Wayland platform by default, which is incompatible with OCCT depending on Xlib;
41+
// Force 'xcb' platform plugin (alternatively, could be passed QApplication as '-platfom xcb' argument).
42+
OSD_Environment aQpaPlat("QT_QPA_PLATFORM");
43+
if (aQpaPlat.Value().IsEmpty())
44+
{
45+
aQpaPlat.SetValue("xcb");
46+
aQpaPlat.Build();
47+
}
48+
#endif
49+
50+
/*#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
51+
// workaround for some bugs in Qt5
52+
OSD_Environment aQGlyph("QT_ENABLE_GLYPH_CACHE_WORKAROUND");
53+
if (aQGlyph.Value().IsEmpty())
54+
{
55+
aQGlyph.SetValue("1");
56+
aQGlyph.Build();
57+
}
58+
#endif*/
59+
60+
// global OpenGL setup managed by Qt
61+
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
62+
QSurfaceFormat::setDefaultFormat(aGlFormat);
63+
64+
// ask Qt managing rendering from GUI thread instead of QSGRenderThread
65+
// for QtQuick applications
66+
/*OSD_Environment aQsgLoop("QSG_RENDER_LOOP");
67+
if (aQsgLoop.Value().IsEmpty())
68+
{
69+
aQsgLoop.SetValue("basic");
70+
aQsgLoop.Build();
71+
}*/
72+
}
73+
2574
// ================================================================
2675
// Function : qtGlSurfaceFormat
2776
// ================================================================

occt-qt-tools/OcctQtTools.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class OpenGl_Caps;
1717
//! Auxiliary tools between Qt and OCCT definitions.
1818
namespace OcctQtTools
1919
{
20+
//! Perform global Qt platform setup - to be called befor QApplication creation.
21+
//! Defines platform plugin to load (e.g. xcb on Linux)
22+
//! and graphic driver (e.g. desktop OpenGL with desired profile/surface).
23+
void qtGlPlatformSetup();
24+
2025
//! Return default Qt surface format for GL context.
2126
QSurfaceFormat qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile = QSurfaceFormat::NoProfile,
2227
bool theToDebug = false);

occt-qtquick/main.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,9 @@
1414

1515
int main(int theNbArgs, char** theArgVec)
1616
{
17-
#if defined(_WIN32)
18-
//
19-
#elif defined(__APPLE__)
20-
//
21-
#else
22-
// Qt6 tries to use Wayland platform by default, which is incompatible with OCCT depending on Xlib;
23-
// Force 'xcb' platform plugin (alternatively, could be passed QApplication as '-platfom xcb' argument).
24-
OSD_Environment aQpaPlat("QT_QPA_PLATFORM");
25-
if (aQpaPlat.Value().IsEmpty())
26-
{
27-
aQpaPlat.SetValue("xcb");
28-
aQpaPlat.Build();
29-
}
30-
#endif
17+
// before creaing QApplication: define platform plugin to load (e.g. xcb on Linux)
18+
// and graphic driver (e.g. desktop OpenGL with desired profile/surface)
19+
OcctQtTools::qtGlPlatformSetup();
3120

3221
// Qt by default will attempt offloading rendering
3322
// into a separate working thread (QSGRenderThread) on some systems,
@@ -49,15 +38,6 @@ int main(int theNbArgs, char** theArgVec)
4938
QCoreApplication::setApplicationVersion(OCC_VERSION_STRING_EXT);
5039
//QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
5140

52-
// OpenGL setup managed by Qt
53-
#if defined(_WIN32)
54-
// never use ANGLE on Windows, since OCCT 3D Viewer does not expect this
55-
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
56-
// QCoreApplication::setAttribute (Qt::AA_UseOpenGLES);
57-
#endif
58-
const QSurfaceFormat aGlFormat = OcctQtTools::qtGlSurfaceFormat();
59-
QSurfaceFormat::setDefaultFormat(aGlFormat);
60-
6141
qmlRegisterType<OcctQQuickFramebufferViewer>("OcctQQuickFramebufferViewer", 1, 0, "OcctQQuickFramebufferViewer");
6242

6343
QQmlApplicationEngine aQmlEngine;

occt-qwidget/main.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <QSlider>
1919
#include <Standard_WarningsRestore.hxx>
2020

21-
#include <OSD_Environment.hxx>
2221
#include <Standard_Version.hxx>
2322

2423
//! Main application window.
@@ -171,21 +170,9 @@ class MyMainWindow : public QMainWindow
171170

172171
int main(int theNbArgs, char** theArgVec)
173172
{
174-
#if defined(_WIN32)
175-
//
176-
#elif defined(__APPLE__)
177-
//
178-
#else
179-
// Qt6 tries to use Wayland platform by default, which is incompatible with OCCT depending on Xlib;
180-
// Force 'xcb' platform plugin (alternatively, could be passed QApplication as '-platfom xcb' argument).
181-
OSD_Environment aQpaPlat("QT_QPA_PLATFORM");
182-
if (aQpaPlat.Value().IsEmpty())
183-
{
184-
aQpaPlat.SetValue("xcb");
185-
aQpaPlat.Build();
186-
}
187-
#endif
188-
173+
// before creaing QApplication: define platform plugin to load (e.g. xcb on Linux)
174+
// and graphic driver (e.g. desktop OpenGL with desired profile/surface)
175+
OcctQtTools::qtGlPlatformSetup();
189176
QApplication aQApp(theNbArgs, theArgVec);
190177

191178
QCoreApplication::setApplicationName("OCCT Qt/QWidget Viewer sample");

0 commit comments

Comments
 (0)