summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/corelib/threads/mandelbrot/main.cpp34
-rw-r--r--examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp16
-rw-r--r--examples/corelib/threads/mandelbrot/mandelbrotwidget.h4
-rw-r--r--examples/corelib/threads/mandelbrot/renderthread.cpp28
-rw-r--r--examples/corelib/threads/mandelbrot/renderthread.h7
-rw-r--r--examples/embedded/flickable/main.cpp2
-rw-r--r--examples/network/fortuneserver/server.cpp2
-rw-r--r--examples/network/threadedfortuneserver/fortuneserver.cpp2
-rw-r--r--examples/network/torrent/torrentclient.cpp16
-rw-r--r--examples/widgets/dialogs/standarddialogs/dialog.cpp8
-rw-r--r--examples/widgets/doc/src/addressbook-tutorial.qdoc6
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.cpp2
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treemodel.cpp3
-rw-r--r--examples/widgets/mainwindows/mainwindow/toolbar.cpp2
14 files changed, 103 insertions, 29 deletions
diff --git a/examples/corelib/threads/mandelbrot/main.cpp b/examples/corelib/threads/mandelbrot/main.cpp
index 9832d55514..19ea8cf417 100644
--- a/examples/corelib/threads/mandelbrot/main.cpp
+++ b/examples/corelib/threads/mandelbrot/main.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -52,11 +52,43 @@
#include <QApplication>
+#include <QScreen>
+
+#include <QCommandLineParser>
+#include <QCommandLineOption>
+#include <QDebug>
+#include <QRect>
+
//! [0]
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
+
+ QCommandLineParser parser;
+ parser.setApplicationDescription("Qt Mandelbrot Example");
+ parser.addHelpOption();
+ parser.addVersionOption();
+ QCommandLineOption passesOption("passes", "Number of passes (1-8)", "passes");
+ parser.addOption(passesOption);
+ parser.process(app);
+
+ if (parser.isSet(passesOption)) {
+ const auto passesStr = parser.value(passesOption);
+ bool ok;
+ const int passes = passesStr.toInt(&ok);
+ if (!ok || passes < 1 || passes > 8) {
+ qWarning() << "Invalid value:" << passesStr;
+ return -1;
+ }
+ RenderThread::setNumPasses(passes);
+ }
+
MandelbrotWidget widget;
+ const auto geometry = widget.screen()->availableGeometry();
+ widget.resize((2 * geometry.size()) / 3);
+ const auto pos = (geometry.size() - widget.size()) / 2;
+ widget.move(geometry.topLeft() + QPoint(pos.width(), pos.height()));
+
widget.show();
return app.exec();
}
diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
index b0c3733b22..bb1f83482b 100644
--- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
+++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -73,6 +73,8 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) :
pixmapScale(DefaultScale),
curScale(DefaultScale)
{
+ help = tr("Use mouse wheel or the '+' and '-' keys to zoom. "
+ "Press and hold left mouse button to scroll.");
connect(&thread, &RenderThread::renderedImage,
this, &MandelbrotWidget::updatePixmap);
@@ -80,8 +82,6 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) :
#if QT_CONFIG(cursor)
setCursor(Qt::CrossCursor);
#endif
- resize(550, 400);
-
}
//! [1]
@@ -127,8 +127,9 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
}
//! [8] //! [9]
- QString text = tr("Use mouse wheel or the '+' and '-' keys to zoom. "
- "Press and hold left mouse button to scroll.");
+ QString text = help;
+ if (!info.isEmpty())
+ text += ' ' + info;
QFontMetrics metrics = painter.fontMetrics();
int textWidth = metrics.horizontalAdvance(text);
@@ -169,6 +170,9 @@ void MandelbrotWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Up:
scroll(0, +ScrollStep);
break;
+ case Qt::Key_Q:
+ close();
+ break;
default:
QWidget::keyPressEvent(event);
}
@@ -226,6 +230,8 @@ void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor)
if (!lastDragPos.isNull())
return;
+ info = image.text(RenderThread::infoKey());
+
pixmap = QPixmap::fromImage(image);
pixmapOffset = QPoint();
lastDragPos = QPoint();
diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h
index cb40962535..956ffc10a8 100644
--- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h
+++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -86,6 +86,8 @@ private:
QPixmap pixmap;
QPoint pixmapOffset;
QPoint lastDragPos;
+ QString help;
+ QString info;
double centerX;
double centerY;
double pixmapScale;
diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp
index 4d2009471c..1f2af3b999 100644
--- a/examples/corelib/threads/mandelbrot/renderthread.cpp
+++ b/examples/corelib/threads/mandelbrot/renderthread.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -51,8 +51,14 @@
#include "renderthread.h"
#include <QImage>
+
+#include <QElapsedTimer>
+#include <QTextStream>
+
#include <cmath>
+int RenderThread::numPasses = 8;
+
//! [0]
RenderThread::RenderThread(QObject *parent)
: QThread(parent)
@@ -98,6 +104,7 @@ void RenderThread::render(double centerX, double centerY, double scaleFactor,
//! [3]
void RenderThread::run()
{
+ QElapsedTimer timer;
forever {
mutex.lock();
const double devicePixelRatio = this->devicePixelRatio;
@@ -116,13 +123,14 @@ void RenderThread::run()
QImage image(resultSize, QImage::Format_RGB32);
image.setDevicePixelRatio(devicePixelRatio);
- const int NumPasses = 8;
int pass = 0;
- while (pass < NumPasses) {
+ while (pass < numPasses) {
const int MaxIterations = (1 << (2 * pass + 6)) + 32;
const int Limit = 4;
bool allBlack = true;
+ timer.restart();
+
for (int y = -halfHeight; y < halfHeight; ++y) {
if (restart)
break;
@@ -165,8 +173,20 @@ void RenderThread::run()
if (allBlack && pass == 0) {
pass = 4;
} else {
- if (!restart)
+ if (!restart) {
+ QString message;
+ QTextStream str(&message);
+ str << " Pass " << (pass + 1) << '/' << numPasses
+ << ", max iterations: " << MaxIterations << ", time: ";
+ const auto elapsed = timer.elapsed();
+ if (elapsed > 2000)
+ str << (elapsed / 1000) << 's';
+ else
+ str << elapsed << "ms";
+ image.setText(infoKey(), message);
+
emit renderedImage(image, requestedScaleFactor);
+ }
//! [5] //! [6]
++pass;
}
diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h
index 6174e0ed3d..7bbd9ae056 100644
--- a/examples/corelib/threads/mandelbrot/renderthread.h
+++ b/examples/corelib/threads/mandelbrot/renderthread.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
@@ -72,6 +72,10 @@ public:
void render(double centerX, double centerY, double scaleFactor, QSize resultSize,
double devicePixelRatio);
+ static void setNumPasses(int n) { numPasses = n; }
+
+ static QString infoKey() { return QStringLiteral("info"); }
+
signals:
void renderedImage(const QImage &image, double scaleFactor);
@@ -88,6 +92,7 @@ private:
double scaleFactor;
double devicePixelRatio;
QSize resultSize;
+ static int numPasses;
bool restart = false;
bool abort = false;
diff --git a/examples/embedded/flickable/main.cpp b/examples/embedded/flickable/main.cpp
index fd30d15e49..139baa6ba7 100644
--- a/examples/embedded/flickable/main.cpp
+++ b/examples/embedded/flickable/main.cpp
@@ -72,7 +72,7 @@ static QStringList colorPairs(int max)
// randomize it
colors.clear();
while (combinedColors.count()) {
- int i = QRandomGenerator::global()->bounded(int(combinedColors.count()));
+ int i = QRandomGenerator::global()->bounded(combinedColors.count());
colors << combinedColors[i];
combinedColors.removeAt(i);
if (colors.count() == max)
diff --git a/examples/network/fortuneserver/server.cpp b/examples/network/fortuneserver/server.cpp
index 95aadb60c9..c91b6a5c0c 100644
--- a/examples/network/fortuneserver/server.cpp
+++ b/examples/network/fortuneserver/server.cpp
@@ -145,7 +145,7 @@ void Server::sendFortune()
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_10);
- out << fortunes[QRandomGenerator::global()->bounded(int(fortunes.size()))];
+ out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
//! [4] //! [7]
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
diff --git a/examples/network/threadedfortuneserver/fortuneserver.cpp b/examples/network/threadedfortuneserver/fortuneserver.cpp
index a93bbc7058..73d7e22531 100644
--- a/examples/network/threadedfortuneserver/fortuneserver.cpp
+++ b/examples/network/threadedfortuneserver/fortuneserver.cpp
@@ -72,7 +72,7 @@ FortuneServer::FortuneServer(QObject *parent)
//! [1]
void FortuneServer::incomingConnection(qintptr socketDescriptor)
{
- QString fortune = fortunes.at(QRandomGenerator::global()->bounded(int(fortunes.size())));
+ QString fortune = fortunes.at(QRandomGenerator::global()->bounded(fortunes.size()));
FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
connect(thread, &FortuneThread::finished, thread, &FortuneThread::deleteLater);
thread->start();
diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp
index a63e927e09..7571d77b41 100644
--- a/examples/network/torrent/torrentclient.cpp
+++ b/examples/network/torrent/torrentclient.cpp
@@ -700,7 +700,7 @@ void TorrentClient::connectToPeers()
d->connections << client;
// Pick a random peer from the list of weighed peers.
- TorrentPeer *peer = weighedPeers.takeAt(QRandomGenerator::global()->bounded(int(weighedPeers.size())));
+ TorrentPeer *peer = weighedPeers.takeAt(QRandomGenerator::global()->bounded(weighedPeers.size()));
weighedPeers.removeAll(peer);
peer->connectStart = QDateTime::currentSecsSinceEpoch();
peer->lastVisited = peer->connectStart;
@@ -1130,7 +1130,7 @@ void TorrentClient::scheduleUploads()
// random peer to allow it to compete for a position among the
// downloaders. (This is known as an "optimistic unchoke".)
if (!allClients.isEmpty()) {
- PeerWireClient *client = allClients[QRandomGenerator::global()->bounded(int(allClients.size()))];
+ PeerWireClient *client = allClients[QRandomGenerator::global()->bounded(allClients.size())];
if (client->peerWireState() & PeerWireClient::ChokingPeer)
client->unchokePeer();
}
@@ -1191,7 +1191,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
piece = d->payloads.value(client);
if (!piece) {
QList<TorrentPiece *> values = d->pendingPieces.values();
- piece = values.value(QRandomGenerator::global()->bounded(int(values.size())));
+ piece = values.value(QRandomGenerator::global()->bounded(values.size()));
piece->inProgress = true;
d->payloads.insert(client, piece);
}
@@ -1248,7 +1248,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
++it;
}
if (!partialPieces.isEmpty())
- piece = partialPieces.value(QRandomGenerator::global()->bounded(int(partialPieces.size())));
+ piece = partialPieces.value(QRandomGenerator::global()->bounded(partialPieces.size()));
if (!piece) {
// Pick a random piece 3 out of 4 times; otherwise, pick either
@@ -1295,7 +1295,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
}
// Select one piece randomly
- pieceIndex = piecesReadyForDownload.at(QRandomGenerator::global()->bounded(int(piecesReadyForDownload.size())));
+ pieceIndex = piecesReadyForDownload.at(QRandomGenerator::global()->bounded(piecesReadyForDownload.size()));
delete [] occurrences;
} else {
// Make up a list of available piece indices, and pick
@@ -1306,7 +1306,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client)
if (incompletePiecesAvailableToClient.testBit(i))
values << i;
}
- pieceIndex = values.at(QRandomGenerator::global()->bounded(int(values.size())));
+ pieceIndex = values.at(QRandomGenerator::global()->bounded(values.size()));
}
// Create a new TorrentPiece and fill in all initial
@@ -1398,8 +1398,8 @@ int TorrentClient::requestBlocks(PeerWireClient *client, TorrentPiece *piece, in
// speedup comes from an increased chance of receiving
// different blocks from the different peers.
for (int i = 0; i < bits.size(); ++i) {
- int a = QRandomGenerator::global()->bounded(int(bits.size()));
- int b = QRandomGenerator::global()->bounded(int(bits.size()));
+ int a = QRandomGenerator::global()->bounded(bits.size());
+ int b = QRandomGenerator::global()->bounded(bits.size());
int tmp = bits[a];
bits[a] = bits[b];
bits[b] = tmp;
diff --git a/examples/widgets/dialogs/standarddialogs/dialog.cpp b/examples/widgets/dialogs/standarddialogs/dialog.cpp
index cd7e8085a2..7df24e06f6 100644
--- a/examples/widgets/dialogs/standarddialogs/dialog.cpp
+++ b/examples/widgets/dialogs/standarddialogs/dialog.cpp
@@ -388,8 +388,14 @@ void Dialog::setColor()
void Dialog::setFont()
{
const QFontDialog::FontDialogOptions options = QFlag(fontDialogOptionsWidget->value());
+
+ const QString &description = fontLabel->text();
+ QFont defaultFont;
+ if (!description.isEmpty())
+ defaultFont.fromString(description);
+
bool ok;
- QFont font = QFontDialog::getFont(&ok, QFont(fontLabel->text()), this, "Select Font", options);
+ QFont font = QFontDialog::getFont(&ok, defaultFont, this, "Select Font", options);
if (ok) {
fontLabel->setText(font.key());
fontLabel->setFont(font);
diff --git a/examples/widgets/doc/src/addressbook-tutorial.qdoc b/examples/widgets/doc/src/addressbook-tutorial.qdoc
index 563b6a2be9..53acb82b1a 100644
--- a/examples/widgets/doc/src/addressbook-tutorial.qdoc
+++ b/examples/widgets/doc/src/addressbook-tutorial.qdoc
@@ -842,7 +842,7 @@
If \c fileName is not empty, we create a QFile object, \c file, with
\c fileName. QFile works with QDataStream as QFile is a QIODevice.
- Next, we attempt to open the file in \l{QIODevice::}{WriteOnly} mode.
+ Next, we attempt to open the file in \l{QIODeviceBase::}{WriteOnly} mode.
If this is unsuccessful, we display a QMessageBox to inform the user.
\snippet tutorials/addressbook/part6/addressbook.cpp saveToFile() function part2
@@ -869,7 +869,7 @@
\image addressbook-tutorial-part6-load.png
If \c fileName is not empty, again, we use a QFile object, \c file, and
- attempt to open it in \l{QIODevice::}{ReadOnly} mode. Similar to our
+ attempt to open it in \l{QIODeviceBase::}{ReadOnly} mode. Similar to our
implementation of \c saveToFile(), if this attempt is unsuccessful, we
display a QMessageBox to inform the user.
@@ -939,7 +939,7 @@
choose a location for the file. Using the file name chosen, we create an
instance of QFile to write to.
- We attempt to open the file in \l{QIODevice::}{WriteOnly} mode. If this
+ We attempt to open the file in \l{QIODeviceBase::}{WriteOnly} mode. If this
process fails, we display a QMessageBox to inform the user about the
problem and return. Otherwise, we pass the file as a parameter to a
QTextStream object, \c out. Like QDataStream, the QTextStream class
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
index 7faa325469..47fc232751 100644
--- a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
@@ -62,7 +62,7 @@ MainWindow::MainWindow(QWidget *parent)
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
- TreeModel *model = new TreeModel(headers, file.readAll());
+ TreeModel *model = new TreeModel(headers, file.readAll(), this);
file.close();
view->setModel(model);
diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
index 1161a8b5d2..e8ca2afffd 100644
--- a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
@@ -212,6 +212,9 @@ bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
//! [8]
int TreeModel::rowCount(const QModelIndex &parent) const
{
+ if (parent.isValid() && parent.column() > 0)
+ return 0;
+
const TreeItem *parentItem = getItem(parent);
return parentItem ? parentItem->childCount() : 0;
diff --git a/examples/widgets/mainwindows/mainwindow/toolbar.cpp b/examples/widgets/mainwindows/mainwindow/toolbar.cpp
index 2c663a1618..431a804298 100644
--- a/examples/widgets/mainwindows/mainwindow/toolbar.cpp
+++ b/examples/widgets/mainwindows/mainwindow/toolbar.cpp
@@ -264,7 +264,7 @@ void ToolBar::randomize()
QList<QAction *> randomized;
QList<QAction *> actions = this->actions();
while (!actions.isEmpty()) {
- QAction *action = actions.takeAt(QRandomGenerator::global()->bounded(int(actions.size())));
+ QAction *action = actions.takeAt(QRandomGenerator::global()->bounded(actions.size()));
randomized.append(action);
}
clear();