From 4d88d79aa507777bce40740b21747f656efc074d Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 2 Aug 2017 11:39:01 +0200 Subject: Update usage of QFontMetrics::width() to new API QFontMetrics(F)::width() has been deprecated and is replaced by horizontalAdvance(). This updates all usage of it in tests and documentation. It is worth noting that many or most of the usages of QFontMetrics::width() probably intended to use boundingRect().width(), but since it currently works, I have not looked into that, just replaced the function name mechanically. Change-Id: Iec382e5bad0b50f37a6cfff841bfb46ed4d4555f Reviewed-by: Lars Knoll --- .../corelib/threads/mandelbrot/mandelbrotwidget.cpp | 2 +- examples/network/torrent/mainwindow.cpp | 18 +++++++++--------- examples/widgets/doc/src/wiggly.qdoc | 8 ++++---- examples/widgets/effects/fademessage/fademessage.cpp | 2 +- examples/widgets/painting/deform/pathdeform.cpp | 2 +- examples/widgets/painting/shared/arthurstyle.cpp | 4 ++-- examples/widgets/statemachine/rogue/window.cpp | 4 ++-- .../widgets/widgets/charactermap/characterwidget.cpp | 2 +- examples/widgets/widgets/codeeditor/codeeditor.cpp | 2 +- examples/widgets/widgets/wiggly/wigglywidget.cpp | 4 ++-- 10 files changed, 24 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index fd0060d8a9..b3e4af5dc8 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -126,7 +126,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) QString text = tr("Use mouse wheel or the '+' and '-' keys to zoom. " "Press and hold left mouse button to scroll."); QFontMetrics metrics = painter.fontMetrics(); - int textWidth = metrics.width(text); + int textWidth = metrics.horizontalAdvance(text); painter.setPen(Qt::NoPen); painter.setBrush(QColor(0, 0, 0, 127)); diff --git a/examples/network/torrent/mainwindow.cpp b/examples/network/torrent/mainwindow.cpp index efdf187174..f80c2c6975 100644 --- a/examples/network/torrent/mainwindow.cpp +++ b/examples/network/torrent/mainwindow.cpp @@ -129,12 +129,12 @@ MainWindow::MainWindow(QWidget *parent) // Set header resize modes and initial section sizes QFontMetrics fm = fontMetrics(); QHeaderView *header = torrentView->header(); - header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent")); - header->resizeSection(1, fm.width(headers.at(1) + " ")); - header->resizeSection(2, fm.width(headers.at(2) + " ")); - header->resizeSection(3, qMax(fm.width(headers.at(3) + " "), fm.width(" 1234.0 KB/s "))); - header->resizeSection(4, qMax(fm.width(headers.at(4) + " "), fm.width(" 1234.0 KB/s "))); - header->resizeSection(5, qMax(fm.width(headers.at(5) + " "), fm.width(tr("Downloading") + " "))); + header->resizeSection(0, fm.horizontalAdvance("typical-name-for-a-torrent.torrent")); + header->resizeSection(1, fm.horizontalAdvance(headers.at(1) + " ")); + header->resizeSection(2, fm.horizontalAdvance(headers.at(2) + " ")); + header->resizeSection(3, qMax(fm.horizontalAdvance(headers.at(3) + " "), fm.horizontalAdvance(" 1234.0 KB/s "))); + header->resizeSection(4, qMax(fm.horizontalAdvance(headers.at(4) + " "), fm.horizontalAdvance(" 1234.0 KB/s "))); + header->resizeSection(5, qMax(fm.horizontalAdvance(headers.at(5) + " "), fm.horizontalAdvance(tr("Downloading") + " "))); // Create common actions QAction *newTorrentAction = new QAction(QIcon(":/icons/bottom.png"), tr("Add &new torrent"), this); @@ -174,14 +174,14 @@ MainWindow::MainWindow(QWidget *parent) bottomBar->addWidget(new QLabel(tr("Max download:"))); bottomBar->addWidget(downloadLimitSlider); bottomBar->addWidget((downloadLimitLabel = new QLabel(tr("0 KB/s")))); - downloadLimitLabel->setFixedSize(QSize(fm.width(tr("99999 KB/s")), fm.lineSpacing())); + downloadLimitLabel->setFixedSize(QSize(fm.horizontalAdvance(tr("99999 KB/s")), fm.lineSpacing())); bottomBar->addSeparator(); uploadLimitSlider = new QSlider(Qt::Horizontal); uploadLimitSlider->setRange(0, 1000); bottomBar->addWidget(new QLabel(tr("Max upload:"))); bottomBar->addWidget(uploadLimitSlider); bottomBar->addWidget((uploadLimitLabel = new QLabel(tr("0 KB/s")))); - uploadLimitLabel->setFixedSize(QSize(fm.width(tr("99999 KB/s")), fm.lineSpacing())); + uploadLimitLabel->setFixedSize(QSize(fm.horizontalAdvance(tr("99999 KB/s")), fm.lineSpacing())); #ifdef Q_OS_OSX setUnifiedTitleAndToolBarOnMac(true); @@ -220,7 +220,7 @@ QSize MainWindow::sizeHint() const // Add up the sizes of all header sections. The last section is // stretched, so its size is relative to the size of the width; // instead of counting it, we count the size of its largest value. - int width = fontMetrics().width(tr("Downloading") + " "); + int width = fontMetrics().horizontalAdvance(tr("Downloading") + " "); for (int i = 0; i < header->count() - 1; ++i) width += header->sectionSize(i); diff --git a/examples/widgets/doc/src/wiggly.qdoc b/examples/widgets/doc/src/wiggly.qdoc index f330b016df..d70d609e2b 100644 --- a/examples/widgets/doc/src/wiggly.qdoc +++ b/examples/widgets/doc/src/wiggly.qdoc @@ -143,10 +143,10 @@ position on the wiggly line based on \c step. In addition, \c x is incremented by the character's width. - For simplicity, we assume that QFontMetrics::width(\c text) - returns the sum of the individual character widths - (QFontMetrics::width(\c text[i])). In practice, this is not - always the case because QFontMetrics::width(\c text) also takes + For simplicity, we assume that QFontMetrics::horizontalAdvance(\c text) + returns the sum of the individual character advances + (QFontMetrics::horizontalAdvance(\c text[i])). In practice, this is not + always the case because QFontMetrics::horizontalAdvance(\c text) also takes into account the kerning between certain letters (e.g., 'A' and 'V'). The result is that the text isn't perfectly centered. You can verify this by typing "AVAVAVAVAVAV" in the line edit. diff --git a/examples/widgets/effects/fademessage/fademessage.cpp b/examples/widgets/effects/fademessage/fademessage.cpp index d91293288c..9943147bb7 100644 --- a/examples/widgets/effects/fademessage/fademessage.cpp +++ b/examples/widgets/effects/fademessage/fademessage.cpp @@ -108,7 +108,7 @@ void FadeMessage::setupScene() int fh = fontMetrics.height(); QString sceneText = "Qt Everywhere!"; - int sceneTextWidth = fontMetrics.width(sceneText); + int sceneTextWidth = fontMetrics.horizontalAdvance(sceneText); QGraphicsRectItem *block = m_scene.addRect(50, 300, sceneTextWidth, fh + 3); block->setPen(Qt::NoPen); diff --git a/examples/widgets/painting/deform/pathdeform.cpp b/examples/widgets/painting/deform/pathdeform.cpp index 3f72a33902..490a8508d6 100644 --- a/examples/widgets/painting/deform/pathdeform.cpp +++ b/examples/widgets/painting/deform/pathdeform.cpp @@ -364,7 +364,7 @@ void PathDeformRenderer::setText(const QString &text) path.addText(advance, f, text.mid(i, 1)); m_pathBounds |= path.boundingRect(); m_paths << path; - advance += QPointF(fm.width(text.mid(i, 1)), 0); + advance += QPointF(fm.horizontalAdvance(text.mid(i, 1)), 0); } } else { QPainterPath path; diff --git a/examples/widgets/painting/shared/arthurstyle.cpp b/examples/widgets/painting/shared/arthurstyle.cpp index 484eaec31c..f4fc76bda6 100644 --- a/examples/widgets/painting/shared/arthurstyle.cpp +++ b/examples/widgets/painting/shared/arthurstyle.cpp @@ -294,7 +294,7 @@ void ArthurStyle::drawComplexControl(ComplexControl control, const QStyleOptionC QPixmap titleLeft = cached(":res/images/title_cap_left.png"); QPixmap titleRight = cached(":res/images/title_cap_right.png"); QPixmap titleStretch = cached(":res/images/title_stretch.png"); - int txt_width = groupBox->fontMetrics.width(groupBox->text) + 20; + int txt_width = groupBox->fontMetrics.horizontalAdvance(groupBox->text) + 20; painter->drawPixmap(r.center().x() - txt_width/2, 0, titleLeft); QRect tileRect = subControlRect(control, groupBox, SC_GroupBoxLabel, widget); painter->drawTiledPixmap(tileRect, titleStretch); @@ -385,7 +385,7 @@ QRect ArthurStyle::subControlRect(ComplexControl control, const QStyleOptionComp QPixmap titleLeft = cached(":res/images/title_cap_left.png"); QPixmap titleRight = cached(":res/images/title_cap_right.png"); QPixmap titleStretch = cached(":res/images/title_stretch.png"); - int txt_width = group->fontMetrics.width(group->text) + 20; + int txt_width = group->fontMetrics.horizontalAdvance(group->text) + 20; rect = QRect(group->rect.center().x() - txt_width/2 + titleLeft.width(), 0, txt_width - titleLeft.width() - titleRight.width(), titleStretch.height()); diff --git a/examples/widgets/statemachine/rogue/window.cpp b/examples/widgets/statemachine/rogue/window.cpp index 3515138382..059fbf1003 100644 --- a/examples/widgets/statemachine/rogue/window.cpp +++ b/examples/widgets/statemachine/rogue/window.cpp @@ -98,7 +98,7 @@ void Window::paintEvent(QPaintEvent * /* event */) QFontMetrics metrics(font()); QPainter painter(this); int fontHeight = metrics.height(); - int fontWidth = metrics.width('X'); + int fontWidth = metrics.horizontalAdvance('X'); int yPos = fontHeight; int xPos; @@ -177,7 +177,7 @@ QSize Window::sizeHint() const { QFontMetrics metrics(font()); - return QSize(metrics.width('X') * WIDTH, metrics.height() * (HEIGHT + 1)); + return QSize(metrics.horizontalAdvance('X') * WIDTH, metrics.height() * (HEIGHT + 1)); } //![2] diff --git a/examples/widgets/widgets/charactermap/characterwidget.cpp b/examples/widgets/widgets/charactermap/characterwidget.cpp index 55d45501f1..5bab921516 100644 --- a/examples/widgets/widgets/charactermap/characterwidget.cpp +++ b/examples/widgets/widgets/charactermap/characterwidget.cpp @@ -181,7 +181,7 @@ void CharacterWidget::paintEvent(QPaintEvent *event) if (key == lastKey) painter.fillRect(column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush(Qt::red)); - painter.drawText(column*squareSize + (squareSize / 2) - fontMetrics.width(QChar(key))/2, + painter.drawText(column*squareSize + (squareSize / 2) - fontMetrics.horizontalAdvance(QChar(key))/2, row*squareSize + 4 + fontMetrics.ascent(), QString(QChar(key))); } diff --git a/examples/widgets/widgets/codeeditor/codeeditor.cpp b/examples/widgets/widgets/codeeditor/codeeditor.cpp index 69974e72be..7e56a75294 100644 --- a/examples/widgets/widgets/codeeditor/codeeditor.cpp +++ b/examples/widgets/widgets/codeeditor/codeeditor.cpp @@ -79,7 +79,7 @@ int CodeEditor::lineNumberAreaWidth() ++digits; } - int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; + int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits; return space; } diff --git a/examples/widgets/widgets/wiggly/wigglywidget.cpp b/examples/widgets/widgets/wiggly/wigglywidget.cpp index 9dfd43ff77..1b8f3cffad 100644 --- a/examples/widgets/widgets/wiggly/wigglywidget.cpp +++ b/examples/widgets/widgets/wiggly/wigglywidget.cpp @@ -77,7 +77,7 @@ void WigglyWidget::paintEvent(QPaintEvent * /* event */) }; QFontMetrics metrics(font()); - int x = (width() - metrics.width(text)) / 2; + int x = (width() - metrics.horizontalAdvance(text)) / 2; int y = (height() + metrics.ascent() - metrics.descent()) / 2; QColor color; //! [2] @@ -91,7 +91,7 @@ void WigglyWidget::paintEvent(QPaintEvent * /* event */) painter.setPen(color); painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400), QString(text[i])); - x += metrics.width(text[i]); + x += metrics.horizontalAdvance(text[i]); } } //! [4] -- cgit v1.2.3