Widgets: use QStringRef to optimize memory allocation
Replace substring functions that return QString with corresponding functions that return QStringRef where it's possible. Create QString from QStringRef only where necessary. Change-Id: Id1c39093199519f2794b11560c2c0ded2d52b928 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
parent
9d0dbc32dd
commit
9b699fa839
@ -3823,11 +3823,11 @@ QString QFileDialogPrivate::getEnvironmentVariable(const QString &string)
|
|||||||
{
|
{
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
if (string.size() > 1 && string.startsWith(QLatin1Char('$'))) {
|
if (string.size() > 1 && string.startsWith(QLatin1Char('$'))) {
|
||||||
return QString::fromLocal8Bit(qgetenv(string.mid(1).toLatin1().constData()));
|
return QString::fromLocal8Bit(qgetenv(string.midRef(1).toLatin1().constData()));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
if (string.size() > 2 && string.startsWith(QLatin1Char('%')) && string.endsWith(QLatin1Char('%'))) {
|
if (string.size() > 2 && string.startsWith(QLatin1Char('%')) && string.endsWith(QLatin1Char('%'))) {
|
||||||
return QString::fromLocal8Bit(qgetenv(string.mid(1, string.size() - 2).toLatin1().constData()));
|
return QString::fromLocal8Bit(qgetenv(string.midRef(1, string.size() - 2).toLatin1().constData()));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return string;
|
return string;
|
||||||
|
@ -1649,7 +1649,7 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio
|
|||||||
|
|
||||||
QRect textRect(xpos, y + windowsItemVMargin, w - xm - windowsRightBorder - tab + 1, h - 2 * windowsItemVMargin);
|
QRect textRect(xpos, y + windowsItemVMargin, w - xm - windowsRightBorder - tab + 1, h - 2 * windowsItemVMargin);
|
||||||
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
|
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
|
||||||
QString s = menuitem->text;
|
QStringRef s(&menuitem->text);
|
||||||
if (!s.isEmpty()) { // draw text
|
if (!s.isEmpty()) { // draw text
|
||||||
p->save();
|
p->save();
|
||||||
int t = s.indexOf(QLatin1Char('\t'));
|
int t = s.indexOf(QLatin1Char('\t'));
|
||||||
@ -1660,12 +1660,13 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio
|
|||||||
if (t >= 0) {
|
if (t >= 0) {
|
||||||
QRect vShortcutRect = visualRect(opt->direction, menuitem->rect,
|
QRect vShortcutRect = visualRect(opt->direction, menuitem->rect,
|
||||||
QRect(textRect.topRight(), QPoint(menuitem->rect.right(), textRect.bottom())));
|
QRect(textRect.topRight(), QPoint(menuitem->rect.right(), textRect.bottom())));
|
||||||
|
const QString textToDraw = s.mid(t + 1).toString();
|
||||||
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, option, widget)) {
|
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, option, widget)) {
|
||||||
p->setPen(menuitem->palette.light().color());
|
p->setPen(menuitem->palette.light().color());
|
||||||
p->drawText(vShortcutRect.adjusted(1, 1, 1, 1), text_flags, s.mid(t + 1));
|
p->drawText(vShortcutRect.adjusted(1, 1, 1, 1), text_flags, textToDraw);
|
||||||
p->setPen(discol);
|
p->setPen(discol);
|
||||||
}
|
}
|
||||||
p->drawText(vShortcutRect, text_flags, s.mid(t + 1));
|
p->drawText(vShortcutRect, text_flags, textToDraw);
|
||||||
s = s.left(t);
|
s = s.left(t);
|
||||||
}
|
}
|
||||||
QFont font = menuitem->font;
|
QFont font = menuitem->font;
|
||||||
@ -1680,12 +1681,13 @@ void QFusionStyle::drawControl(ControlElement element, const QStyleOption *optio
|
|||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
|
|
||||||
p->setFont(font);
|
p->setFont(font);
|
||||||
|
const QString textToDraw = s.left(t).toString();
|
||||||
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, option, widget)) {
|
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, option, widget)) {
|
||||||
p->setPen(menuitem->palette.light().color());
|
p->setPen(menuitem->palette.light().color());
|
||||||
p->drawText(vTextRect.adjusted(1, 1, 1, 1), text_flags, s.left(t));
|
p->drawText(vTextRect.adjusted(1, 1, 1, 1), text_flags, textToDraw);
|
||||||
p->setPen(discol);
|
p->setPen(discol);
|
||||||
}
|
}
|
||||||
p->drawText(vTextRect, text_flags, s.left(t));
|
p->drawText(vTextRect, text_flags, textToDraw);
|
||||||
p->restore();
|
p->restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3679,7 +3679,7 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
|
|||||||
|
|
||||||
QRect textRect = subRule.contentsRect(opt->rect);
|
QRect textRect = subRule.contentsRect(opt->rect);
|
||||||
textRect.setWidth(textRect.width() - mi.tabWidth);
|
textRect.setWidth(textRect.width() - mi.tabWidth);
|
||||||
QString s = mi.text;
|
QStringRef s(&mi.text);
|
||||||
p->setPen(mi.palette.buttonText().color());
|
p->setPen(mi.palette.buttonText().color());
|
||||||
if (!s.isEmpty()) {
|
if (!s.isEmpty()) {
|
||||||
int text_flags = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine;
|
int text_flags = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine;
|
||||||
@ -3689,10 +3689,10 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q
|
|||||||
if (t >= 0) {
|
if (t >= 0) {
|
||||||
QRect vShortcutRect = visualRect(opt->direction, mi.rect,
|
QRect vShortcutRect = visualRect(opt->direction, mi.rect,
|
||||||
QRect(textRect.topRight(), QPoint(mi.rect.right(), textRect.bottom())));
|
QRect(textRect.topRight(), QPoint(mi.rect.right(), textRect.bottom())));
|
||||||
p->drawText(vShortcutRect, text_flags, s.mid(t + 1));
|
p->drawText(vShortcutRect, text_flags, s.mid(t + 1).toString());
|
||||||
s = s.left(t);
|
s = s.left(t);
|
||||||
}
|
}
|
||||||
p->drawText(textRect, text_flags, s.left(t));
|
p->drawText(textRect, text_flags, s.left(t).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mi.menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
|
if (mi.menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
|
||||||
|
@ -1198,7 +1198,7 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
|
|||||||
QRect textRect(xpos, y + QWindowsStylePrivate::windowsItemVMargin,
|
QRect textRect(xpos, y + QWindowsStylePrivate::windowsItemVMargin,
|
||||||
w - xm - QWindowsStylePrivate::windowsRightBorder - tab + 1, h - 2 * QWindowsStylePrivate::windowsItemVMargin);
|
w - xm - QWindowsStylePrivate::windowsRightBorder - tab + 1, h - 2 * QWindowsStylePrivate::windowsItemVMargin);
|
||||||
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
|
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
|
||||||
QString s = menuitem->text;
|
QStringRef s(&menuitem->text);
|
||||||
if (!s.isEmpty()) { // draw text
|
if (!s.isEmpty()) { // draw text
|
||||||
p->save();
|
p->save();
|
||||||
int t = s.indexOf(QLatin1Char('\t'));
|
int t = s.indexOf(QLatin1Char('\t'));
|
||||||
@ -1209,24 +1209,26 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
|
|||||||
if (t >= 0) {
|
if (t >= 0) {
|
||||||
QRect vShortcutRect = visualRect(opt->direction, menuitem->rect,
|
QRect vShortcutRect = visualRect(opt->direction, menuitem->rect,
|
||||||
QRect(textRect.topRight(), QPoint(menuitem->rect.right(), textRect.bottom())));
|
QRect(textRect.topRight(), QPoint(menuitem->rect.right(), textRect.bottom())));
|
||||||
|
const QString textToDraw = s.mid(t + 1).toString();
|
||||||
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
|
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
|
||||||
p->setPen(menuitem->palette.light().color());
|
p->setPen(menuitem->palette.light().color());
|
||||||
p->drawText(vShortcutRect.adjusted(1,1,1,1), text_flags, s.mid(t + 1));
|
p->drawText(vShortcutRect.adjusted(1, 1, 1, 1), text_flags, textToDraw);
|
||||||
p->setPen(discol);
|
p->setPen(discol);
|
||||||
}
|
}
|
||||||
p->drawText(vShortcutRect, text_flags, s.mid(t + 1));
|
p->drawText(vShortcutRect, text_flags, textToDraw);
|
||||||
s = s.left(t);
|
s = s.left(t);
|
||||||
}
|
}
|
||||||
QFont font = menuitem->font;
|
QFont font = menuitem->font;
|
||||||
if (menuitem->menuItemType == QStyleOptionMenuItem::DefaultItem)
|
if (menuitem->menuItemType == QStyleOptionMenuItem::DefaultItem)
|
||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
p->setFont(font);
|
p->setFont(font);
|
||||||
|
const QString textToDraw = s.left(t).toString();
|
||||||
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
|
if (dis && !act && proxy()->styleHint(SH_EtchDisabledText, opt, widget)) {
|
||||||
p->setPen(menuitem->palette.light().color());
|
p->setPen(menuitem->palette.light().color());
|
||||||
p->drawText(vTextRect.adjusted(1,1,1,1), text_flags, s.left(t));
|
p->drawText(vTextRect.adjusted(1, 1, 1, 1), text_flags, textToDraw);
|
||||||
p->setPen(discol);
|
p->setPen(discol);
|
||||||
}
|
}
|
||||||
p->drawText(vTextRect, text_flags, s.left(t));
|
p->drawText(vTextRect, text_flags, textToDraw);
|
||||||
p->restore();
|
p->restore();
|
||||||
}
|
}
|
||||||
if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
|
if (menuitem->menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
|
||||||
|
@ -554,7 +554,7 @@ void QCalendarDateValidator::setFormat(const QString &format)
|
|||||||
bool quoting = false;
|
bool quoting = false;
|
||||||
QString separator;
|
QString separator;
|
||||||
while (pos < format.size()) {
|
while (pos < format.size()) {
|
||||||
QString mid = format.mid(pos);
|
const QStringRef mid = format.midRef(pos);
|
||||||
int offset = 1;
|
int offset = 1;
|
||||||
|
|
||||||
if (mid.startsWith(quote)) {
|
if (mid.startsWith(quote)) {
|
||||||
|
@ -97,7 +97,7 @@ void QLineEditPrivate::_q_completionHighlighted(const QString &newText)
|
|||||||
} else {
|
} else {
|
||||||
int c = control->cursor();
|
int c = control->cursor();
|
||||||
QString text = control->text();
|
QString text = control->text();
|
||||||
q->setText(text.left(c) + newText.mid(c));
|
q->setText(text.leftRef(c) + newText.midRef(c));
|
||||||
control->moveCursor(control->end(), false);
|
control->moveCursor(control->end(), false);
|
||||||
#ifndef Q_OS_ANDROID
|
#ifndef Q_OS_ANDROID
|
||||||
const bool mark = true;
|
const bool mark = true;
|
||||||
|
@ -1180,14 +1180,14 @@ QString QWidgetLineControl::maskString(uint pos, const QString &str, bool clear)
|
|||||||
int n = findInMask(i, true, true, str[(int)strIndex]);
|
int n = findInMask(i, true, true, str[(int)strIndex]);
|
||||||
if (n != -1) {
|
if (n != -1) {
|
||||||
if (str.length() != 1 || i == 0 || (i > 0 && (!m_maskData[i-1].separator || m_maskData[i-1].maskChar != str[(int)strIndex]))) {
|
if (str.length() != 1 || i == 0 || (i > 0 && (!m_maskData[i-1].separator || m_maskData[i-1].maskChar != str[(int)strIndex]))) {
|
||||||
s += fill.mid(i, n-i+1);
|
s += fill.midRef(i, n - i + 1);
|
||||||
i = n + 1; // update i to find + 1
|
i = n + 1; // update i to find + 1
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// search for valid m_blank if not
|
// search for valid m_blank if not
|
||||||
n = findInMask(i, true, false, str[(int)strIndex]);
|
n = findInMask(i, true, false, str[(int)strIndex]);
|
||||||
if (n != -1) {
|
if (n != -1) {
|
||||||
s += fill.mid(i, n-i);
|
s += fill.midRef(i, n - i);
|
||||||
switch (m_maskData[n].caseMode) {
|
switch (m_maskData[n].caseMode) {
|
||||||
case MaskInputData::Upper:
|
case MaskInputData::Upper:
|
||||||
s += str[(int)strIndex].toUpper();
|
s += str[(int)strIndex].toUpper();
|
||||||
|
@ -2123,7 +2123,7 @@ QVariant QWidgetTextControl::inputMethodQuery(Qt::InputMethodQuery property, QVa
|
|||||||
tmpCursor.movePosition(QTextCursor::NextBlock);
|
tmpCursor.movePosition(QTextCursor::NextBlock);
|
||||||
--numBlocks;
|
--numBlocks;
|
||||||
}
|
}
|
||||||
result += block.text().mid(0,localPos);
|
result += block.text().midRef(0, localPos);
|
||||||
return QVariant(result);
|
return QVariant(result);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user