diff options
author | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-03-24 16:10:15 +0100 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-03-24 16:10:15 +0100 |
commit | 3b5c0bc0780f1749fed7c07bd8b691400a0282b7 (patch) | |
tree | 1022f5553ad5a0aca9b5f3b49ca38a01c2329d20 /examples | |
parent | c79918733a194ebbe5a2fe1617c884659f3e4b9f (diff) | |
parent | 21f1738a94fc8544ece04b3b1ee03a11986fe59b (diff) |
Merge remote-tracking branch 'origin/stable' into dev
Conflicts:
src/gui/image/qjpeghandler.cpp
Change-Id: I9db3acea7d5c82f5da679c8eaeb29431136665f0
Diffstat (limited to 'examples')
-rw-r--r-- | examples/gui/analogclock/main.cpp | 2 | ||||
-rw-r--r-- | examples/network/dnslookup/dnslookup.cpp | 171 | ||||
-rw-r--r-- | examples/network/dnslookup/dnslookup.h | 20 | ||||
-rw-r--r-- | examples/opengl/contextinfo/renderwindow.cpp | 9 | ||||
-rw-r--r-- | examples/opengl/contextinfo/widget.cpp | 41 | ||||
-rw-r--r-- | examples/opengl/contextinfo/widget.h | 4 | ||||
-rw-r--r-- | examples/opengl/cube/mainwidget.cpp | 7 | ||||
-rw-r--r-- | examples/widgets/mainwindows/application/mainwindow.cpp | 11 |
8 files changed, 182 insertions, 83 deletions
diff --git a/examples/gui/analogclock/main.cpp b/examples/gui/analogclock/main.cpp index e33aba83fd..1e10fcd07b 100644 --- a/examples/gui/analogclock/main.cpp +++ b/examples/gui/analogclock/main.cpp @@ -156,5 +156,5 @@ int main(int argc, char **argv) AnalogClockWindow clock; clock.show(); - app.exec(); + return app.exec(); } diff --git a/examples/network/dnslookup/dnslookup.cpp b/examples/network/dnslookup/dnslookup.cpp index 202a5f9580..003a3e3028 100644 --- a/examples/network/dnslookup/dnslookup.cpp +++ b/examples/network/dnslookup/dnslookup.cpp @@ -45,71 +45,114 @@ #include <QHostAddress> #include <QStringList> #include <QTimer> +#include <QCommandLineParser> +#include <QCommandLineOption> #include <stdio.h> -static void usage() { - printf("Qt DNS example - performs DNS lookups\n" - "Usage: dnslookup [-t <type>] [-s nameserver] name\n\n"); +static int typeFromParameter(const QString &type) +{ + if (type == "a") + return QDnsLookup::A; + if (type == "aaaa") + return QDnsLookup::AAAA; + if (type == "any") + return QDnsLookup::ANY; + if (type == "cname") + return QDnsLookup::CNAME; + if (type == "mx") + return QDnsLookup::MX; + if (type == "ns") + return QDnsLookup::NS; + if (type == "ptr") + return QDnsLookup::PTR; + if (type == "srv") + return QDnsLookup::SRV; + if (type == "txt") + return QDnsLookup::TXT; + return -1; } -DnsManager::DnsManager() +//! [0] + +enum CommandLineParseResult { - dns = new QDnsLookup(this); - connect(dns, SIGNAL(finished()), this, SLOT(showResults())); -} + CommandLineOk, + CommandLineError, + CommandLineVersionRequested, + CommandLineHelpRequested +}; -void DnsManager::execute() +CommandLineParseResult parseCommandLine(QCommandLineParser &parser, DnsQuery *query, QString *errorMessage) { - QStringList args = QCoreApplication::instance()->arguments(); - args.takeFirst(); + parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); + const QCommandLineOption nameServerOption("n", "The name server to use.", "nameserver"); + parser.addOption(nameServerOption); + const QCommandLineOption typeOption("t", "The lookup type.", "type"); + parser.addOption(typeOption); + parser.addPositionalArgument("name", "The name to look up."); + const QCommandLineOption helpOption = parser.addHelpOption(); + const QCommandLineOption versionOption = parser.addVersionOption(); + + if (!parser.parse(QCoreApplication::arguments())) { + *errorMessage = parser.errorText(); + return CommandLineError; + } - // lookup type - dns->setType(QDnsLookup::A); - if (args.size() > 1 && args.first() == "-t") { - args.takeFirst(); - const QString type = args.takeFirst().toLower(); - if (type == "a") - dns->setType(QDnsLookup::A); - else if (type == "aaaa") - dns->setType(QDnsLookup::AAAA); - else if (type == "any") - dns->setType(QDnsLookup::ANY); - else if (type == "cname") - dns->setType(QDnsLookup::CNAME); - else if (type == "mx") - dns->setType(QDnsLookup::MX); - else if (type == "ns") - dns->setType(QDnsLookup::NS); - else if (type == "ptr") - dns->setType(QDnsLookup::PTR); - else if (type == "srv") - dns->setType(QDnsLookup::SRV); - else if (type == "txt") - dns->setType(QDnsLookup::TXT); - else { - printf("Bad record type: %s\n", qPrintable(type)); - QCoreApplication::instance()->quit(); - return; + if (parser.isSet(versionOption)) + return CommandLineVersionRequested; + + if (parser.isSet(helpOption)) + return CommandLineHelpRequested; + + if (parser.isSet(nameServerOption)) { + const QString nameserver = parser.value(nameServerOption); + query->nameServer = QHostAddress(nameserver); + if (query->nameServer.isNull() || query->nameServer.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) { + *errorMessage = "Bad nameserver address: " + nameserver; + return CommandLineError; } } - if (args.size() > 1 && args.first() == "-s") { - args.takeFirst(); - const QString ns = args.takeFirst(); - QHostAddress nameserver(ns); - if (nameserver.isNull() || nameserver.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) { - printf("Bad nameserver address: %s\n", qPrintable(ns)); - QCoreApplication::instance()->quit(); - return; + + if (parser.isSet(typeOption)) { + const QString typeParameter = parser.value(typeOption); + const int type = typeFromParameter(typeParameter.toLower()); + if (type < 0) { + *errorMessage = "Bad record type: " + typeParameter; + return CommandLineError; } - dns->setNameserver(nameserver); + query->type = static_cast<QDnsLookup::Type>(type); } - if (args.isEmpty()) { - usage(); - QCoreApplication::instance()->quit(); - return; + + const QStringList positionalArguments = parser.positionalArguments(); + if (positionalArguments.isEmpty()) { + *errorMessage = "Argument 'name' missing."; + return CommandLineError; + } + if (positionalArguments.size() > 1) { + *errorMessage = "Several 'name' arguments specified."; + return CommandLineError; } - dns->setName(args.takeFirst()); + query->name = positionalArguments.first(); + + return CommandLineOk; +} + +//! [0] + +DnsManager::DnsManager() +{ + dns = new QDnsLookup(this); + connect(dns, SIGNAL(finished()), this, SLOT(showResults())); +} + +void DnsManager::execute() +{ + // lookup type + dns->setType(query.type); + if (!query.nameServer.isNull()) + dns->setNameserver(query.nameServer); + dns->setName(query.name); dns->lookup(); } @@ -159,7 +202,33 @@ int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); +//! [1] + QCoreApplication::setApplicationVersion(QT_VERSION_STR); + QCoreApplication::setApplicationName(QCoreApplication::translate("QDnsLookupExample", "DNS Lookup Example")); + QCommandLineParser parser; + parser.setApplicationDescription(QCoreApplication::translate("QDnsLookupExample", "An example demonstrating the class QDnsLookup.")); + DnsQuery query; + QString errorMessage; + switch (parseCommandLine(parser, &query, &errorMessage)) { + case CommandLineOk: + break; + case CommandLineError: + fputs(qPrintable(errorMessage), stderr); + fputs("\n\n", stderr); + fputs(qPrintable(parser.helpText()), stderr); + return 1; + case CommandLineVersionRequested: + printf("%s %s\n", qPrintable(QCoreApplication::applicationName()), + qPrintable(QCoreApplication::applicationVersion())); + return 0; + case CommandLineHelpRequested: + parser.showHelp(); + Q_UNREACHABLE(); + } +//! [1] + DnsManager manager; + manager.setQuery(query); QTimer::singleShot(0, &manager, SLOT(execute())); return app.exec(); diff --git a/examples/network/dnslookup/dnslookup.h b/examples/network/dnslookup/dnslookup.h index d76756bad0..4d0232be8a 100644 --- a/examples/network/dnslookup/dnslookup.h +++ b/examples/network/dnslookup/dnslookup.h @@ -38,11 +38,21 @@ ** ****************************************************************************/ -#include <QObject> +#include <QDnsLookup> +#include <QHostAddress> -QT_BEGIN_NAMESPACE -class QDnsLookup; -QT_END_NAMESPACE +//! [0] + +struct DnsQuery +{ + DnsQuery() : type(QDnsLookup::A) {} + + QDnsLookup::Type type; + QHostAddress nameServer; + QString name; +}; + +//! [0] class DnsManager : public QObject { @@ -50,6 +60,7 @@ class DnsManager : public QObject public: DnsManager(); + void setQuery(const DnsQuery &q) { query = q; } public slots: void execute(); @@ -57,5 +68,6 @@ public slots: private: QDnsLookup *dns; + DnsQuery query; }; diff --git a/examples/opengl/contextinfo/renderwindow.cpp b/examples/opengl/contextinfo/renderwindow.cpp index 85fb19bd1a..af51de1d67 100644 --- a/examples/opengl/contextinfo/renderwindow.cpp +++ b/examples/opengl/contextinfo/renderwindow.cpp @@ -174,7 +174,7 @@ void RenderWindow::setupVertexAttribs() void RenderWindow::render() { if (!m_context->makeCurrent(this)) { - qWarning("makeCurrent() failed"); + emit error(tr("makeCurrent() failed")); return; } @@ -216,5 +216,10 @@ void RenderWindow::render() m_context->swapBuffers(this); m_angle += 1.0f; - QTimer::singleShot(0, this, SLOT(render())); + + // Instead of 0 wait a few more milliseconds before rendering again. This is + // only here to make the UI widgets more responsive on slower machines. We + // can afford it since our rendering is so lightweight. + const int interval = 5; + QTimer::singleShot(interval, this, SLOT(render())); } diff --git a/examples/opengl/contextinfo/widget.cpp b/examples/opengl/contextinfo/widget.cpp index ff78639e24..4ab62ec2d2 100644 --- a/examples/opengl/contextinfo/widget.cpp +++ b/examples/opengl/contextinfo/widget.cpp @@ -275,6 +275,7 @@ void Widget::start() addRenderWindow(); return; } + m_surface = renderWindow; renderWindow->setForceGLSL110(forceGLSL110); connect(renderWindow, &RenderWindow::ready, this, &Widget::renderWindowReady); @@ -284,12 +285,8 @@ void Widget::start() addRenderWindow(); } -void Widget::renderWindowReady() +void Widget::printFormat(const QSurfaceFormat &format) { - QOpenGLContext *context = QOpenGLContext::currentContext(); - Q_ASSERT(context); - const QSurfaceFormat format = context->format(); - m_output->append(tr("OpenGL version: %1.%2").arg(format.majorVersion()).arg(format.minorVersion())); for (size_t i = 0; i < sizeof(profiles) / sizeof(Profile); ++i) @@ -310,6 +307,21 @@ void Widget::renderWindowReady() break; } + m_output->append(tr("Depth buffer size: %1").arg(QString::number(format.depthBufferSize()))); + m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize()))); + m_output->append(tr("Samples: %1").arg(QString::number(format.samples()))); + m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize()))); + m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize()))); + m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize()))); + m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize()))); + m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval()))); +} + +void Widget::renderWindowReady() +{ + QOpenGLContext *context = QOpenGLContext::currentContext(); + Q_ASSERT(context); + QString vendor, renderer, version, glslVersion; const GLubyte *p; QOpenGLFunctions *f = context->functions(); @@ -322,22 +334,21 @@ void Widget::renderWindowReady() if ((p = f->glGetString(GL_SHADING_LANGUAGE_VERSION))) glslVersion = QString::fromLatin1(reinterpret_cast<const char *>(p)); - m_output->append(tr("\nVendor: %1").arg(vendor)); + m_output->append(tr("*** Context information ***")); + m_output->append(tr("Vendor: %1").arg(vendor)); m_output->append(tr("Renderer: %1").arg(renderer)); m_output->append(tr("OpenGL version: %1").arg(version)); m_output->append(tr("GLSL version: %1").arg(glslVersion)); - m_output->append(tr("\nDepth buffer size: %1").arg(QString::number(format.depthBufferSize()))); - m_output->append(tr("Stencil buffer size: %1").arg(QString::number(format.stencilBufferSize()))); - m_output->append(tr("Samples: %1").arg(QString::number(format.samples()))); - m_output->append(tr("Red buffer size: %1").arg(QString::number(format.redBufferSize()))); - m_output->append(tr("Green buffer size: %1").arg(QString::number(format.greenBufferSize()))); - m_output->append(tr("Blue buffer size: %1").arg(QString::number(format.blueBufferSize()))); - m_output->append(tr("Alpha buffer size: %1").arg(QString::number(format.alphaBufferSize()))); - m_output->append(tr("Swap interval: %1").arg(QString::number(format.swapInterval()))); + m_output->append(tr("\n*** QSurfaceFormat from context ***")); + printFormat(context->format()); + + m_output->append(tr("\n*** QSurfaceFormat from window surface ***")); + printFormat(m_surface->format()); + m_output->append(tr("\n*** Qt build information ***")); const char *gltype[] = { "Desktop", "GLES 2", "GLES 1" }; - m_output->append(tr("\nQt OpenGL configuration: %1") + m_output->append(tr("Qt OpenGL configuration: %1") .arg(QString::fromLatin1(gltype[QOpenGLContext::openGLModuleType()]))); m_output->append(tr("Qt OpenGL library handle: %1") .arg(QString::number(qintptr(QOpenGLContext::openGLModuleHandle()), 16))); diff --git a/examples/opengl/contextinfo/widget.h b/examples/opengl/contextinfo/widget.h index dcae66dac0..ad664178e4 100644 --- a/examples/opengl/contextinfo/widget.h +++ b/examples/opengl/contextinfo/widget.h @@ -46,6 +46,8 @@ QT_FORWARD_DECLARE_CLASS(QComboBox) QT_FORWARD_DECLARE_CLASS(QTextEdit) QT_FORWARD_DECLARE_CLASS(QVBoxLayout) +QT_FORWARD_DECLARE_CLASS(QSurfaceFormat) +QT_FORWARD_DECLARE_CLASS(QSurface) class Widget : public QWidget { @@ -65,6 +67,7 @@ private: void addOptions(QLayout *layout); void addRenderableTypes(QLayout *layout); void addRenderWindow(); + void printFormat(const QSurfaceFormat &format); QComboBox *m_version; QLayout *m_profiles; @@ -74,6 +77,7 @@ private: QTextEdit *m_extensions; QVBoxLayout *m_renderWindowLayout; QWidget *m_renderWindowContainer; + QSurface *m_surface; }; #endif // WIDGET_H diff --git a/examples/opengl/cube/mainwidget.cpp b/examples/opengl/cube/mainwidget.cpp index 8c87de6736..5c1cd28b54 100644 --- a/examples/opengl/cube/mainwidget.cpp +++ b/examples/opengl/cube/mainwidget.cpp @@ -43,7 +43,6 @@ #include <QMouseEvent> #include <math.h> -#include <locale.h> MainWidget::MainWidget(QWidget *parent) : QGLWidget(parent), @@ -126,9 +125,6 @@ void MainWidget::initializeGL() //! [3] void MainWidget::initShaders() { - // Override system locale until shaders are compiled - setlocale(LC_NUMERIC, "C"); - // Compile vertex shader if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl")) close(); @@ -144,9 +140,6 @@ void MainWidget::initShaders() // Bind shader pipeline for use if (!program.bind()) close(); - - // Restore system locale - setlocale(LC_ALL, ""); } //! [3] diff --git a/examples/widgets/mainwindows/application/mainwindow.cpp b/examples/widgets/mainwindows/application/mainwindow.cpp index 8bdd0303de..a5a0def0a6 100644 --- a/examples/widgets/mainwindows/application/mainwindow.cpp +++ b/examples/widgets/mainwindows/application/mainwindow.cpp @@ -118,11 +118,16 @@ bool MainWindow::save() bool MainWindow::saveAs() //! [11] //! [12] { - QString fileName = QFileDialog::getSaveFileName(this); - if (fileName.isEmpty()) + QFileDialog dialog(this); + dialog.setWindowModality(Qt::WindowModal); + dialog.setAcceptMode(QFileDialog::AcceptSave); + dialog.exec(); + QStringList files = dialog.selectedFiles(); + + if (files.isEmpty()) return false; - return saveFile(fileName); + return saveFile(files.at(0)); } //! [12] |