merge bitcoin#16701: Replace functions deprecated in Qt 5.13

This commit is contained in:
Kittywhiskers Van Gogh 2021-12-05 18:45:22 +05:30
parent 16b2591e77
commit d4a1decb73
7 changed files with 34 additions and 18 deletions

View File

@ -117,7 +117,7 @@ public:
ensurePolished(); ensurePolished();
const QFontMetrics fm(fontMetrics()); const QFontMetrics fm(fontMetrics());
int h = 0; int h = 0;
int w = fm.width(BitcoinUnits::format(BitcoinUnits::DASH, BitcoinUnits::maxMoney(), false, BitcoinUnits::separatorAlways)); int w = GUIUtil::TextWidth(fm, BitcoinUnits::format(BitcoinUnits::DASH, BitcoinUnits::maxMoney(), false, BitcoinUnits::separatorAlways));
w += 2; // cursor blinking space w += 2; // cursor blinking space
w += GUIUtil::dashThemeActive() ? 24 : 0; // counteract padding from css w += GUIUtil::dashThemeActive() ? 24 : 0; // counteract padding from css
return QSize(w, h); return QSize(w, h);

View File

@ -46,7 +46,6 @@
#include <QButtonGroup> #include <QButtonGroup>
#include <QComboBox> #include <QComboBox>
#include <QDateTime> #include <QDateTime>
#include <QDesktopWidget>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QListWidget> #include <QListWidget>
#include <QMenu> #include <QMenu>
@ -54,6 +53,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QMimeData> #include <QMimeData>
#include <QProgressDialog> #include <QProgressDialog>
#include <QScreen>
#include <QSettings> #include <QSettings>
#include <QShortcut> #include <QShortcut>
#include <QStackedWidget> #include <QStackedWidget>
@ -89,7 +89,7 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const NetworkStyle* networkStyle,
QSettings settings; QSettings settings;
if (!restoreGeometry(settings.value("MainWindowGeometry").toByteArray())) { if (!restoreGeometry(settings.value("MainWindowGeometry").toByteArray())) {
// Restore failed (perhaps missing setting), center the window // Restore failed (perhaps missing setting), center the window
move(QApplication::desktop()->availableGeometry().center() - frameGeometry().center()); move(QGuiApplication::primaryScreen()->availableGeometry().center() - frameGeometry().center());
} }
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
@ -1324,7 +1324,7 @@ void BitcoinGUI::updateWidth()
continue; continue;
} }
QFontMetrics fm(button->font()); QFontMetrics fm(button->font());
nWidthWidestButton = std::max<int>(nWidthWidestButton, fm.width(button->text())); nWidthWidestButton = std::max<int>(nWidthWidestButton, GUIUtil::TextWidth(fm, button->text()));
++nButtonsVisible; ++nButtonsVisible;
} }
// Add 30 per button as padding and use minimum 980 which is the minimum required to show all tab's contents // Add 30 per button as padding and use minimum 980 which is the minimum required to show all tab's contents
@ -1990,7 +1990,7 @@ UnitDisplayStatusBarControl::UnitDisplayStatusBarControl() :
const QFontMetrics fm(GUIUtil::getFontNormal()); const QFontMetrics fm(GUIUtil::getFontNormal());
for (const BitcoinUnits::Unit unit : units) for (const BitcoinUnits::Unit unit : units)
{ {
max_width = qMax(max_width, fm.width(BitcoinUnits::name(unit))); max_width = qMax(max_width, GUIUtil::TextWidth(fm, BitcoinUnits::name(unit)));
} }
setMinimumSize(max_width, 0); setMinimumSize(max_width, 0);
setAlignment(Qt::AlignRight | Qt::AlignVCenter); setAlignment(Qt::AlignRight | Qt::AlignVCenter);

View File

@ -51,7 +51,6 @@
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
#include <QDesktopServices> #include <QDesktopServices>
#include <QDesktopWidget>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QDoubleValidator> #include <QDoubleValidator>
#include <QFileDialog> #include <QFileDialog>
@ -1832,7 +1831,7 @@ qreal calculateIdealFontSize(int width, const QString& text, QFont font, qreal m
while(font_size >= minPointSize) { while(font_size >= minPointSize) {
font.setPointSizeF(font_size); font.setPointSizeF(font_size);
QFontMetrics fm(font); QFontMetrics fm(font);
if (fm.width(text) < width) { if (TextWidth(fm, text) < width) {
break; break;
} }
font_size -= 0.5; font_size -= 0.5;
@ -1872,4 +1871,13 @@ void PolishProgressDialog(QProgressDialog* dialog)
#endif #endif
} }
int TextWidth(const QFontMetrics& fm, const QString& text)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
return fm.horizontalAdvance(text);
#else
return fm.width(text);
#endif
}
} // namespace GUIUtil } // namespace GUIUtil

View File

@ -467,6 +467,14 @@ namespace GUIUtil
// Fix known bugs in QProgressDialog class. // Fix known bugs in QProgressDialog class.
void PolishProgressDialog(QProgressDialog* dialog); void PolishProgressDialog(QProgressDialog* dialog);
/**
* Returns the distance in pixels appropriate for drawing a subsequent character after text.
*
* In Qt 5.12 and before the QFontMetrics::width() is used and it is deprecated since Qt 13.0.
* In Qt 5.11 the QFontMetrics::horizontalAdvance() was introduced.
*/
int TextWidth(const QFontMetrics& fm, const QString& text);
} // namespace GUIUtil } // namespace GUIUtil
#endif // BITCOIN_QT_GUIUTIL_H #endif // BITCOIN_QT_GUIUTIL_H

View File

@ -497,7 +497,7 @@ void OptionsDialog::updateWidth()
continue; continue;
} }
QFontMetrics fm(button->font()); QFontMetrics fm(button->font());
nWidthWidestButton = std::max<int>(nWidthWidestButton, fm.width(button->text())); nWidthWidestButton = std::max<int>(nWidthWidestButton, GUIUtil::TextWidth(fm, button->text()));
++nButtonsVisible; ++nButtonsVisible;
} }
// Add 10 per button as padding and use minimum 585 which is what we used in css before // Add 10 per button as padding and use minimum 585 which is what we used in css before

View File

@ -31,12 +31,12 @@
#include <QButtonGroup> #include <QButtonGroup>
#include <QDir> #include <QDir>
#include <QDesktopWidget>
#include <QFontDatabase> #include <QFontDatabase>
#include <QKeyEvent> #include <QKeyEvent>
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QScrollBar> #include <QScrollBar>
#include <QScreen>
#include <QSettings> #include <QSettings>
#include <QTime> #include <QTime>
#include <QTimer> #include <QTimer>
@ -466,7 +466,7 @@ RPCConsole::RPCConsole(interfaces::Node& node, QWidget* parent, Qt::WindowFlags
QSettings settings; QSettings settings;
if (!restoreGeometry(settings.value("RPCConsoleWindowGeometry").toByteArray())) { if (!restoreGeometry(settings.value("RPCConsoleWindowGeometry").toByteArray())) {
// Restore failed (perhaps missing setting), center the window // Restore failed (perhaps missing setting), center the window
move(QApplication::desktop()->availableGeometry().center() - frameGeometry().center()); move(QGuiApplication::primaryScreen()->availableGeometry().center() - frameGeometry().center());
} }
QChar nonbreaking_hyphen(8209); QChar nonbreaking_hyphen(8209);

View File

@ -9,22 +9,22 @@
#include <qt/splashscreen.h> #include <qt/splashscreen.h>
#include <qt/guiutil.h>
#include <qt/networkstyle.h>
#include <chainparams.h> #include <chainparams.h>
#include <clientversion.h> #include <clientversion.h>
#include <interfaces/handler.h> #include <interfaces/handler.h>
#include <interfaces/node.h> #include <interfaces/node.h>
#include <interfaces/wallet.h> #include <interfaces/wallet.h>
#include <qt/guiutil.h>
#include <qt/networkstyle.h>
#include <ui_interface.h> #include <ui_interface.h>
#include <util/system.h> #include <util/system.h>
#include <version.h> #include <version.h>
#include <QApplication> #include <QApplication>
#include <QCloseEvent> #include <QCloseEvent>
#include <QDesktopWidget>
#include <QPainter> #include <QPainter>
#include <QScreen>
SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) : SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const NetworkStyle *networkStyle) :
@ -89,7 +89,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
fontBold.setPointSize(50 * fontFactor); fontBold.setPointSize(50 * fontFactor);
pixPaint.setFont(fontBold); pixPaint.setFont(fontBold);
QFontMetrics fm = pixPaint.fontMetrics(); QFontMetrics fm = pixPaint.fontMetrics();
int titleTextWidth = fm.width(titleText); int titleTextWidth = GUIUtil::TextWidth(fm, titleText);
if (titleTextWidth > width * 0.8) { if (titleTextWidth > width * 0.8) {
fontFactor = 0.75; fontFactor = 0.75;
} }
@ -97,14 +97,14 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
fontBold.setPointSize(50 * fontFactor); fontBold.setPointSize(50 * fontFactor);
pixPaint.setFont(fontBold); pixPaint.setFont(fontBold);
fm = pixPaint.fontMetrics(); fm = pixPaint.fontMetrics();
titleTextWidth = fm.width(titleText); titleTextWidth = GUIUtil::TextWidth(fm, titleText);
int titleTextHeight = fm.height(); int titleTextHeight = fm.height();
pixPaint.drawText((width / 2) - (titleTextWidth / 2), titleTextHeight + paddingTop, titleText); pixPaint.drawText((width / 2) - (titleTextWidth / 2), titleTextHeight + paddingTop, titleText);
fontNormal.setPointSize(16 * fontFactor); fontNormal.setPointSize(16 * fontFactor);
pixPaint.setFont(fontNormal); pixPaint.setFont(fontNormal);
fm = pixPaint.fontMetrics(); fm = pixPaint.fontMetrics();
int versionTextWidth = fm.width(versionText); int versionTextWidth = GUIUtil::TextWidth(fm, versionText);
pixPaint.drawText((width / 2) - (versionTextWidth / 2), titleTextHeight + paddingTop + titleVersionVSpace, versionText); pixPaint.drawText((width / 2) - (versionTextWidth / 2), titleTextHeight + paddingTop + titleVersionVSpace, versionText);
// draw additional text if special network // draw additional text if special network
@ -112,7 +112,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
fontBold.setPointSize(10 * fontFactor); fontBold.setPointSize(10 * fontFactor);
pixPaint.setFont(fontBold); pixPaint.setFont(fontBold);
fm = pixPaint.fontMetrics(); fm = pixPaint.fontMetrics();
int titleAddTextWidth = fm.width(titleAddText); int titleAddTextWidth = GUIUtil::TextWidth(fm, titleAddText);
// Draw the badge background with the network-specific color // Draw the badge background with the network-specific color
QRect badgeRect = QRect(width - titleAddTextWidth - 20, 5, width, fm.height() + 10); QRect badgeRect = QRect(width - titleAddTextWidth - 20, 5, width, fm.height() + 10);
QColor badgeColor = networkStyle->getBadgeColor(); QColor badgeColor = networkStyle->getBadgeColor();
@ -128,7 +128,7 @@ SplashScreen::SplashScreen(interfaces::Node& node, Qt::WindowFlags f, const Netw
QRect r(QPoint(), QSize(width, height)); QRect r(QPoint(), QSize(width, height));
resize(r.size()); resize(r.size());
setFixedSize(r.size()); setFixedSize(r.size());
move(QApplication::desktop()->screenGeometry().center() - r.center()); move(QGuiApplication::primaryScreen()->geometry().center() - r.center());
subscribeToCoreSignals(); subscribeToCoreSignals();
installEventFilter(this); installEventFilter(this);