diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-11-30 12:31:11 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-12-15 17:50:38 +0000 |
commit | 03356fd17a5611911e620dbb65f8bb578fdffafb (patch) | |
tree | 0510cce7c2406796c3751ad5f693adea241ff6f3 /src/widgets | |
parent | 786984e7e47a63094ad64ec86a4892cc5c0ad6d4 (diff) |
QMessageBox: optimize textToCopy string construction
1. Keep 'separator' a QLatin1String.
- saves at least two memory allocations
- necessitates carrying the \n previously prepended to it
around explicitly
2. Start adding to 'textToCopy' with op+=
- saves one allocation, costs one -> ±0
- preallocates more capacity than if we started with
assignment
3. Collapse three unconditional op+= into one
- more efficient usage of QStringBuilder
4. Don't collect button texts in a separate variable, but
append to 'textToCopy' directly.
- saves at least one memory allocation, probably more
since the growth increments of 'textToCopy' should
be larger (due to more content) than those of a new
variable.
Also replace index-based iteration over the buttons with
C++11 range-for over a const QList. Avoids the detach that
happened previously, due to use of op[] instead of at(),
but frankly, I was just too lazy to separate this change.
Change-Id: I27a46a6a163c16d773124f140e085325b17ce5d1
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/dialogs/qmessagebox.cpp | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/widgets/dialogs/qmessagebox.cpp b/src/widgets/dialogs/qmessagebox.cpp index 4ad235051c..76a9de0f1c 100644 --- a/src/widgets/dialogs/qmessagebox.cpp +++ b/src/widgets/dialogs/qmessagebox.cpp @@ -1478,24 +1478,21 @@ void QMessageBox::keyPressEvent(QKeyEvent *e) #if defined(Q_OS_WIN) if (e == QKeySequence::Copy) { - QString separator = QString::fromLatin1("---------------------------\n"); - QString textToCopy = separator; - separator.prepend(QLatin1Char('\n')); - textToCopy += windowTitle() + separator; // title - textToCopy += d->label->text() + separator; // text + const QLatin1String separator("---------------------------\n"); + QString textToCopy; + textToCopy += separator + windowTitle() + QLatin1Char('\n') + separator // title + + d->label->text() + QLatin1Char('\n') + separator; // text if (d->informativeLabel) - textToCopy += d->informativeLabel->text() + separator; + textToCopy += d->informativeLabel->text() + QLatin1Char('\n') + separator; - QString buttonTexts; - QList<QAbstractButton *> buttons = d->buttonBox->buttons(); - for (int i = 0; i < buttons.count(); i++) { - buttonTexts += buttons[i]->text() + QLatin1String(" "); - } - textToCopy += buttonTexts + separator; + const QList<QAbstractButton *> buttons = d->buttonBox->buttons(); + for (const auto *button : buttons) + textToCopy += button->text() + QLatin1String(" "); + textToCopy += QLatin1Char('\n') + separator; #ifndef QT_NO_TEXTEDIT if (d->detailsText) - textToCopy += d->detailsText->text() + separator; + textToCopy += d->detailsText->text() + QLatin1Char('\n') + separator; #endif QApplication::clipboard()->setText(textToCopy); return; |