From f2e3fdc803b9842b6ba4aefe7becdd42eafc4d38 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 23 Jan 2015 11:29:55 +0100 Subject: examples: migrate to QString::asprintf Not that many, if any, uses of sprintf here were idiomatic Qt, but that's for another commit. Change-Id: Ic34470d9799942f786770ba9541b29c34d67c6f8 Reviewed-by: Thiago Macieira --- examples/embedded/flickable/main.cpp | 5 ++--- examples/network/torrent/addtorrentdialog.cpp | 10 ++++------ examples/network/torrent/connectionmanager.cpp | 4 +--- examples/network/torrent/mainwindow.cpp | 12 +++++------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/examples/embedded/flickable/main.cpp b/examples/embedded/flickable/main.cpp index 6d8cc779dd..15b282aaa5 100644 --- a/examples/embedded/flickable/main.cpp +++ b/examples/embedded/flickable/main.cpp @@ -79,9 +79,8 @@ public: QStringList colors = colorPairs(999); for (int i = 0; i < colors.count(); ++i) { - QString c = colors[i]; - QString str; - str.sprintf("%4d", i + 1); + const QString c = colors[i]; + const QString str = QString::asprintf("%4d", i + 1); m_colorNames << (str + " " + c); QStringList duet = c.split(' '); diff --git a/examples/network/torrent/addtorrentdialog.cpp b/examples/network/torrent/addtorrentdialog.cpp index 4c63d3293e..a0d21598c3 100644 --- a/examples/network/torrent/addtorrentdialog.cpp +++ b/examples/network/torrent/addtorrentdialog.cpp @@ -48,16 +48,14 @@ static QString stringNumber(qint64 number) { - QString tmp; if (number > (1024 * 1024 * 1024)) - tmp.sprintf("%.2fGB", number / (1024.0 * 1024.0 * 1024.0)); + return QString::asprintf("%.2fGB", number / (1024.0 * 1024.0 * 1024.0)); else if (number > (1024 * 1024)) - tmp.sprintf("%.2fMB", number / (1024.0 * 1024.0)); + return QString::asprintf("%.2fMB", number / (1024.0 * 1024.0)); else if (number > (1024)) - tmp.sprintf("%.2fKB", number / (1024.0)); + return QString::asprintf("%.2fKB", number / (1024.0)); else - tmp.sprintf("%d bytes", int(number)); - return tmp; + return QString::asprintf("%d bytes", int(number)); } AddTorrentDialog::AddTorrentDialog(QWidget *parent) diff --git a/examples/network/torrent/connectionmanager.cpp b/examples/network/torrent/connectionmanager.cpp index b21ed79394..134eb7eca2 100644 --- a/examples/network/torrent/connectionmanager.cpp +++ b/examples/network/torrent/connectionmanager.cpp @@ -78,9 +78,7 @@ QByteArray ConnectionManager::clientId() const // Generate peer id int startupTime = int(QDateTime::currentDateTime().toTime_t()); - QString s; - s.sprintf("-QT%04x-", (QT_VERSION % 0xffff00) >> 8); - id += s.toLatin1(); + id += QString::asprintf("-QT%04x-", (QT_VERSION % 0xffff00) >> 8).toLatin1(); id += QByteArray::number(startupTime, 10); id += QByteArray(20 - id.size(), '-'); } diff --git a/examples/network/torrent/mainwindow.cpp b/examples/network/torrent/mainwindow.cpp index 0b493fb42b..653882cfb8 100644 --- a/examples/network/torrent/mainwindow.cpp +++ b/examples/network/torrent/mainwindow.cpp @@ -92,7 +92,7 @@ public: // Set the progress and text values of the style option. int progress = qobject_cast(parent())->clientForRow(index.row())->progress(); progressBarOption.progress = progress < 0 ? 0 : progress; - progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress); + progressBarOption.text = QString::asprintf("%d%%", progressBarOption.progress); // Draw the progress bar onto the view. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); @@ -509,8 +509,7 @@ void MainWindow::updateDownloadRate(int bytesPerSecond) // Update the download rate. TorrentClient *client = qobject_cast(sender()); int row = rowOfClient(client); - QString num; - num.sprintf("%.1f KB/s", bytesPerSecond / 1024.0); + const QString num = QString::asprintf("%.1f KB/s", bytesPerSecond / 1024.0); torrentView->topLevelItem(row)->setText(3, num); if (!saveChanges) { @@ -524,8 +523,7 @@ void MainWindow::updateUploadRate(int bytesPerSecond) // Update the upload rate. TorrentClient *client = qobject_cast(sender()); int row = rowOfClient(client); - QString num; - num.sprintf("%.1f KB/s", bytesPerSecond / 1024.0); + const QString num = QString::asprintf("%.1f KB/s", bytesPerSecond / 1024.0); torrentView->topLevelItem(row)->setText(4, num); if (!saveChanges) { @@ -593,14 +591,14 @@ static int rateFromValue(int value) void MainWindow::setUploadLimit(int value) { int rate = rateFromValue(value); - uploadLimitLabel->setText(tr("%1 KB/s").arg(QString().sprintf("%4d", rate))); + uploadLimitLabel->setText(tr("%1 KB/s").arg(QString::asprintf("%4d", rate))); RateController::instance()->setUploadLimit(rate * 1024); } void MainWindow::setDownloadLimit(int value) { int rate = rateFromValue(value); - downloadLimitLabel->setText(tr("%1 KB/s").arg(QString().sprintf("%4d", rate))); + downloadLimitLabel->setText(tr("%1 KB/s").arg(QString::asprintf("%4d", rate))); RateController::instance()->setDownloadLimit(rate * 1024); } -- cgit v1.2.3