From b3717fc7f01ae078faf066538ddc80fca016ef0c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 29 Aug 2017 16:21:17 +0200 Subject: OpenGL examples: Introduce QCommandLineParser Task-number: QTBUG-60626 Change-Id: I6d102327c89206fcdce10f3ac04e112270b11ad2 Reviewed-by: Laszlo Agocs --- examples/opengl/hellogl2/glwidget.cpp | 5 +++-- examples/opengl/hellogl2/glwidget.h | 5 ++++- examples/opengl/hellogl2/main.cpp | 27 +++++++++++++++++++++++--- examples/opengl/hellowindow/main.cpp | 27 ++++++++++++++++++++++---- examples/opengl/qopenglwidget/main.cpp | 15 +++++++++++++- examples/opengl/threadedqopenglwidget/main.cpp | 15 +++++++++++++- 6 files changed, 82 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/opengl/hellogl2/glwidget.cpp b/examples/opengl/hellogl2/glwidget.cpp index fc961da4c4..f9518ffac5 100644 --- a/examples/opengl/hellogl2/glwidget.cpp +++ b/examples/opengl/hellogl2/glwidget.cpp @@ -54,6 +54,8 @@ #include #include +bool GLWidget::m_transparent = false; + GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent), m_xRot(0), @@ -61,10 +63,9 @@ GLWidget::GLWidget(QWidget *parent) m_zRot(0), m_program(0) { - m_core = QCoreApplication::arguments().contains(QStringLiteral("--coreprofile")); + m_core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile; // --transparent causes the clear color to be transparent. Therefore, on systems that // support it, the widget will become transparent apart from the logo. - m_transparent = QCoreApplication::arguments().contains(QStringLiteral("--transparent")); if (m_transparent) { QSurfaceFormat fmt = format(); fmt.setAlphaBufferSize(8); diff --git a/examples/opengl/hellogl2/glwidget.h b/examples/opengl/hellogl2/glwidget.h index cff5633893..21dd200dc7 100644 --- a/examples/opengl/hellogl2/glwidget.h +++ b/examples/opengl/hellogl2/glwidget.h @@ -68,6 +68,9 @@ public: GLWidget(QWidget *parent = 0); ~GLWidget(); + static bool isTransparent() { return m_transparent; } + static void setTransparent(bool t) { m_transparent = t; } + QSize minimumSizeHint() const override; QSize sizeHint() const override; @@ -108,7 +111,7 @@ private: QMatrix4x4 m_proj; QMatrix4x4 m_camera; QMatrix4x4 m_world; - bool m_transparent; + static bool m_transparent; }; #endif diff --git a/examples/opengl/hellogl2/main.cpp b/examples/opengl/hellogl2/main.cpp index 143f659eb6..b52a5a37d3 100644 --- a/examples/opengl/hellogl2/main.cpp +++ b/examples/opengl/hellogl2/main.cpp @@ -51,25 +51,46 @@ #include #include #include +#include +#include +#include "glwidget.h" #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); + QCoreApplication::setApplicationName("Qt Hello GL 2 Example"); + QCoreApplication::setOrganizationName("QtProject"); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::applicationName()); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption multipleSampleOption("multisample", "Multisampling"); + parser.addOption(multipleSampleOption); + QCommandLineOption coreProfileOption("coreprofile", "Use core profile"); + parser.addOption(coreProfileOption); + QCommandLineOption transparentOption("transparent", "Transparent window"); + parser.addOption(transparentOption); + + parser.process(app); + QSurfaceFormat fmt; fmt.setDepthBufferSize(24); - if (QCoreApplication::arguments().contains(QStringLiteral("--multisample"))) + if (parser.isSet(multipleSampleOption)) fmt.setSamples(4); - if (QCoreApplication::arguments().contains(QStringLiteral("--coreprofile"))) { + if (parser.isSet(coreProfileOption)) { fmt.setVersion(3, 2); fmt.setProfile(QSurfaceFormat::CoreProfile); } QSurfaceFormat::setDefaultFormat(fmt); MainWindow mainWindow; - if (QCoreApplication::arguments().contains(QStringLiteral("--transparent"))) { + + GLWidget::setTransparent(parser.isSet(transparentOption)); + if (GLWidget::isTransparent()) { mainWindow.setAttribute(Qt::WA_TranslucentBackground); mainWindow.setAttribute(Qt::WA_NoSystemBackground, false); } diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp index a20e7002a1..7f0be39f5d 100644 --- a/examples/opengl/hellowindow/main.cpp +++ b/examples/opengl/hellowindow/main.cpp @@ -52,6 +52,8 @@ #include +#include +#include #include #include #include @@ -60,9 +62,26 @@ int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); + QCoreApplication::setApplicationName("Qt HelloWindow GL Example"); + QCoreApplication::setOrganizationName("QtProject"); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::applicationName()); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption multipleOption("multiple", "Create multiple windows"); + parser.addOption(multipleOption); + QCommandLineOption multipleSampleOption("multisample", "Multisampling"); + parser.addOption(multipleSampleOption); + QCommandLineOption multipleScreenOption("multiscreen", "Run on multiple screens"); + parser.addOption(multipleScreenOption); + QCommandLineOption timeoutOption("timeout", "Close after 10s"); + parser.addOption(timeoutOption); + parser.process(app); + // Some platforms can only have one window per screen. Therefore we need to differentiate. - const bool multipleWindows = QGuiApplication::arguments().contains(QStringLiteral("--multiple")); - const bool multipleScreens = QGuiApplication::arguments().contains(QStringLiteral("--multiscreen")); + const bool multipleWindows = parser.isSet(multipleOption); + const bool multipleScreens = parser.isSet(multipleScreenOption); QScreen *screen = QGuiApplication::primaryScreen(); @@ -70,7 +89,7 @@ int main(int argc, char *argv[]) QSurfaceFormat format; format.setDepthBufferSize(16); - if (QGuiApplication::arguments().contains(QStringLiteral("--multisample"))) + if (parser.isSet(multipleSampleOption)) format.setSamples(4); QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80); @@ -136,7 +155,7 @@ int main(int argc, char *argv[]) } // Quit after 10 seconds. For platforms that do not have windows that are closeable. - if (QCoreApplication::arguments().contains(QStringLiteral("--timeout"))) + if (parser.isSet(timeoutOption)) QTimer::singleShot(10000, qGuiApp, &QCoreApplication::quit); const int exitValue = app.exec(); diff --git a/examples/opengl/qopenglwidget/main.cpp b/examples/opengl/qopenglwidget/main.cpp index d7aa791142..ea90dca62f 100644 --- a/examples/opengl/qopenglwidget/main.cpp +++ b/examples/opengl/qopenglwidget/main.cpp @@ -51,6 +51,8 @@ #include #include #include +#include +#include #include "mainwindow.h" int main( int argc, char ** argv ) @@ -58,10 +60,21 @@ int main( int argc, char ** argv ) Q_INIT_RESOURCE(texture); QApplication a( argc, argv ); + QCoreApplication::setApplicationName("Qt QOpenGLWidget Example"); + QCoreApplication::setOrganizationName("QtProject"); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::applicationName()); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption multipleSampleOption("multisample", "Multisampling"); + parser.addOption(multipleSampleOption); + parser.process(a); + QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); - if (QCoreApplication::arguments().contains(QStringLiteral("--multisample"))) + if (parser.isSet(multipleSampleOption)) format.setSamples(4); QSurfaceFormat::setDefaultFormat(format); diff --git a/examples/opengl/threadedqopenglwidget/main.cpp b/examples/opengl/threadedqopenglwidget/main.cpp index c22cee9228..b9e491040f 100644 --- a/examples/opengl/threadedqopenglwidget/main.cpp +++ b/examples/opengl/threadedqopenglwidget/main.cpp @@ -53,6 +53,8 @@ #include #include #include +#include +#include #include "mainwindow.h" #include "glwidget.h" @@ -67,6 +69,17 @@ int main( int argc, char ** argv ) { QApplication a( argc, argv ); + QCoreApplication::setApplicationName("Qt Threaded QOpenGLWidget Example"); + QCoreApplication::setOrganizationName("QtProject"); + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::applicationName()); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption singleOption("single", "Single thread"); + parser.addOption(singleOption); + parser.process(a); + QSurfaceFormat format; format.setDepthBufferSize(16); QSurfaceFormat::setDefaultFormat(format); @@ -93,7 +106,7 @@ int main( int argc, char ** argv ) QScopedPointer mw1; QScopedPointer mw2; - if (!QApplication::arguments().contains(QStringLiteral("--single"))) { + if (!parser.isSet(singleOption)) { if (supportsThreading) { pos += QPoint(100, 100); mw1.reset(new MainWindow); -- cgit v1.2.3 From e29b72384b66ac5fba3b287c8f6a3376d0aff852 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 29 Aug 2017 16:19:09 +0200 Subject: Hello GL2 example: Fix exit crash Bail out of cleanup() when run 2nd time. Task-number: QTBUG-60626 Change-Id: I8a9be2fcfb0e8a5584ce8ed7952affff24bd2a33 Reviewed-by: Laszlo Agocs --- examples/opengl/hellogl2/glwidget.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/opengl/hellogl2/glwidget.cpp b/examples/opengl/hellogl2/glwidget.cpp index f9518ffac5..318adb5043 100644 --- a/examples/opengl/hellogl2/glwidget.cpp +++ b/examples/opengl/hellogl2/glwidget.cpp @@ -128,6 +128,8 @@ void GLWidget::setZRotation(int angle) void GLWidget::cleanup() { + if (m_program == nullptr) + return; makeCurrent(); m_logoVbo.destroy(); delete m_program; -- cgit v1.2.3