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>
This commit is contained in:
Marc Mutz 2015-11-30 12:31:11 +01:00
parent 786984e7e4
commit 03356fd17a

View File

@ -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;