OpenGL examples: Introduce QCommandLineParser

Task-number: QTBUG-60626
Change-Id: I6d102327c89206fcdce10f3ac04e112270b11ad2
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Friedemann Kleint 2017-08-29 16:21:17 +02:00
parent d64940891d
commit b3717fc7f0
6 changed files with 82 additions and 12 deletions

View File

@ -54,6 +54,8 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <math.h> #include <math.h>
bool GLWidget::m_transparent = false;
GLWidget::GLWidget(QWidget *parent) GLWidget::GLWidget(QWidget *parent)
: QOpenGLWidget(parent), : QOpenGLWidget(parent),
m_xRot(0), m_xRot(0),
@ -61,10 +63,9 @@ GLWidget::GLWidget(QWidget *parent)
m_zRot(0), m_zRot(0),
m_program(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 // --transparent causes the clear color to be transparent. Therefore, on systems that
// support it, the widget will become transparent apart from the logo. // support it, the widget will become transparent apart from the logo.
m_transparent = QCoreApplication::arguments().contains(QStringLiteral("--transparent"));
if (m_transparent) { if (m_transparent) {
QSurfaceFormat fmt = format(); QSurfaceFormat fmt = format();
fmt.setAlphaBufferSize(8); fmt.setAlphaBufferSize(8);

View File

@ -68,6 +68,9 @@ public:
GLWidget(QWidget *parent = 0); GLWidget(QWidget *parent = 0);
~GLWidget(); ~GLWidget();
static bool isTransparent() { return m_transparent; }
static void setTransparent(bool t) { m_transparent = t; }
QSize minimumSizeHint() const override; QSize minimumSizeHint() const override;
QSize sizeHint() const override; QSize sizeHint() const override;
@ -108,7 +111,7 @@ private:
QMatrix4x4 m_proj; QMatrix4x4 m_proj;
QMatrix4x4 m_camera; QMatrix4x4 m_camera;
QMatrix4x4 m_world; QMatrix4x4 m_world;
bool m_transparent; static bool m_transparent;
}; };
#endif #endif

View File

@ -51,25 +51,46 @@
#include <QApplication> #include <QApplication>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QSurfaceFormat> #include <QSurfaceFormat>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "glwidget.h"
#include "mainwindow.h" #include "mainwindow.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, 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; QSurfaceFormat fmt;
fmt.setDepthBufferSize(24); fmt.setDepthBufferSize(24);
if (QCoreApplication::arguments().contains(QStringLiteral("--multisample"))) if (parser.isSet(multipleSampleOption))
fmt.setSamples(4); fmt.setSamples(4);
if (QCoreApplication::arguments().contains(QStringLiteral("--coreprofile"))) { if (parser.isSet(coreProfileOption)) {
fmt.setVersion(3, 2); fmt.setVersion(3, 2);
fmt.setProfile(QSurfaceFormat::CoreProfile); fmt.setProfile(QSurfaceFormat::CoreProfile);
} }
QSurfaceFormat::setDefaultFormat(fmt); QSurfaceFormat::setDefaultFormat(fmt);
MainWindow mainWindow; 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_TranslucentBackground);
mainWindow.setAttribute(Qt::WA_NoSystemBackground, false); mainWindow.setAttribute(Qt::WA_NoSystemBackground, false);
} }

View File

@ -52,6 +52,8 @@
#include <qpa/qplatformintegration.h> #include <qpa/qplatformintegration.h>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QGuiApplication> #include <QGuiApplication>
#include <QScreen> #include <QScreen>
#include <QThread> #include <QThread>
@ -60,9 +62,26 @@ int main(int argc, char *argv[])
{ {
QGuiApplication app(argc, 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. // Some platforms can only have one window per screen. Therefore we need to differentiate.
const bool multipleWindows = QGuiApplication::arguments().contains(QStringLiteral("--multiple")); const bool multipleWindows = parser.isSet(multipleOption);
const bool multipleScreens = QGuiApplication::arguments().contains(QStringLiteral("--multiscreen")); const bool multipleScreens = parser.isSet(multipleScreenOption);
QScreen *screen = QGuiApplication::primaryScreen(); QScreen *screen = QGuiApplication::primaryScreen();
@ -70,7 +89,7 @@ int main(int argc, char *argv[])
QSurfaceFormat format; QSurfaceFormat format;
format.setDepthBufferSize(16); format.setDepthBufferSize(16);
if (QGuiApplication::arguments().contains(QStringLiteral("--multisample"))) if (parser.isSet(multipleSampleOption))
format.setSamples(4); format.setSamples(4);
QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80); 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. // 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); QTimer::singleShot(10000, qGuiApp, &QCoreApplication::quit);
const int exitValue = app.exec(); const int exitValue = app.exec();

View File

@ -51,6 +51,8 @@
#include <QApplication> #include <QApplication>
#include <QMainWindow> #include <QMainWindow>
#include <QSurfaceFormat> #include <QSurfaceFormat>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "mainwindow.h" #include "mainwindow.h"
int main( int argc, char ** argv ) int main( int argc, char ** argv )
@ -58,10 +60,21 @@ int main( int argc, char ** argv )
Q_INIT_RESOURCE(texture); Q_INIT_RESOURCE(texture);
QApplication a( argc, argv ); 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; QSurfaceFormat format;
format.setDepthBufferSize(24); format.setDepthBufferSize(24);
format.setStencilBufferSize(8); format.setStencilBufferSize(8);
if (QCoreApplication::arguments().contains(QStringLiteral("--multisample"))) if (parser.isSet(multipleSampleOption))
format.setSamples(4); format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format); QSurfaceFormat::setDefaultFormat(format);

View File

@ -53,6 +53,8 @@
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QSurfaceFormat> #include <QSurfaceFormat>
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "mainwindow.h" #include "mainwindow.h"
#include "glwidget.h" #include "glwidget.h"
@ -67,6 +69,17 @@ int main( int argc, char ** argv )
{ {
QApplication a( argc, 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; QSurfaceFormat format;
format.setDepthBufferSize(16); format.setDepthBufferSize(16);
QSurfaceFormat::setDefaultFormat(format); QSurfaceFormat::setDefaultFormat(format);
@ -93,7 +106,7 @@ int main( int argc, char ** argv )
QScopedPointer<MainWindow> mw1; QScopedPointer<MainWindow> mw1;
QScopedPointer<MainWindow> mw2; QScopedPointer<MainWindow> mw2;
if (!QApplication::arguments().contains(QStringLiteral("--single"))) { if (!parser.isSet(singleOption)) {
if (supportsThreading) { if (supportsThreading) {
pos += QPoint(100, 100); pos += QPoint(100, 100);
mw1.reset(new MainWindow); mw1.reset(new MainWindow);