From: Pasi Kallinen Date: Sun, 15 Aug 2021 07:58:39 +0000 (+0300) Subject: Qt: deprecation warnings, again X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=98ec66e00cf5ce91ecb8633d377908015f386bc3;p=nethack Qt: deprecation warnings, again Check Qt version for the QFontMetrics::width vs horizontalAdvance. Of course can't just #define horizontalAdvance to width, that would be too easy ... --- diff --git a/win/Qt/qt_map.cpp b/win/Qt/qt_map.cpp index 344899425..7ab314bd7 100644 --- a/win/Qt/qt_map.cpp +++ b/win/Qt/qt_map.cpp @@ -145,7 +145,7 @@ void NetHackQtMapViewport::SetupTextmapFont(QPainter &painter) QFont f(fontfamily, pts, maybebold); painter.setFont(QFont(fontfamily, pts)); QFontMetrics fm = painter.fontMetrics(); - if (fm.horizontalAdvance("M") > qt_settings->glyphs().width()) + if (fm.QFM_WIDTH("M") > qt_settings->glyphs().width()) break; if (fm.height() > qt_settings->glyphs().height()) break; diff --git a/win/Qt/qt_menu.cpp b/win/Qt/qt_menu.cpp index 860ac764c..682a911cf 100644 --- a/win/Qt/qt_menu.cpp +++ b/win/Qt/qt_menu.cpp @@ -379,7 +379,7 @@ void NetHackQtMenuWindow::PadMenuColumns(bool split_descr) QString col0width_str = ""; if (biggestcount > 0L) col0width_str = QString::asprintf("%*ld", std::max(countdigits, 1), biggestcount); - int col0width_int = (int) fm.horizontalAdvance(col0width_str) + MENU_WIDTH_SLOP; + int col0width_int = (int) fm.QFM_WIDTH(col0width_str) + MENU_WIDTH_SLOP; if (col0width_int > table->columnWidth(0)) WidenColumn(0, col0width_int); @@ -401,7 +401,7 @@ void NetHackQtMenuWindow::PadMenuColumns(bool split_descr) QStringList columns = itemlist[row].str.split("\t"); for (int fld = 0; fld < (int) columns.size(); ++fld) { bool lastcol = (fld == (int) columns.size() - 1); - int w = fm.horizontalAdvance(columns[fld] + (lastcol ? "" : " ")); + int w = fm.QFM_WIDTH(columns[fld] + (lastcol ? "" : " ")); if (fld >= (int) col_widths.size()) { col_widths.push_back(w); // add another element } else if (col_widths[fld] < w) { @@ -435,14 +435,14 @@ void NetHackQtMenuWindow::PadMenuColumns(bool split_descr) for (int fld = 0; fld < (int) columns.size() - 1; ++fld) { //columns[fld] += "\t"; /* (used to pad with tabs) */ int width = col_widths[fld]; - while (fm.horizontalAdvance(columns[fld]) < width) + while (fm.QFM_WIDTH(columns[fld]) < width) columns[fld] += " "; //"\t"; } text = columns.join(""); twi->setText(text); } // TODO? if description needs to wrap, increase the height of this row - int wid = fm.horizontalAdvance(text) + MENU_WIDTH_SLOP; + int wid = fm.QFM_WIDTH(text) + MENU_WIDTH_SLOP; if (wid > widest4) widest4 = wid; } @@ -552,7 +552,7 @@ void NetHackQtMenuWindow::AddRow(int row, const MenuItem& mi) table->setItem(row, 0, twi); twi->setFlags(Qt::ItemIsEnabled); #if 0 // active count field now widened as needed rather than preset - WidenColumn(0, fm.horizontalAdvance("999999") + MENU_WIDTH_SLOP); + WidenColumn(0, fm.QFM_WIDTH("999999") + MENU_WIDTH_SLOP); #else WidenColumn(0, MENU_WIDTH_SLOP); #endif @@ -600,7 +600,7 @@ void NetHackQtMenuWindow::AddRow(int row, const MenuItem& mi) // for the normal case of "a - ", the trailing space hid the fact that // the column wasn't wide enough for four characters; for the " #" // and " *" cases, the last character was replaced by very tiny "..." - int w = (int) fm.horizontalAdvance(letter); + int w = (int) fm.QFM_WIDTH(letter); if (w) w += MENU_WIDTH_SLOP / 2; WidenColumn(3, w); @@ -608,7 +608,7 @@ void NetHackQtMenuWindow::AddRow(int row, const MenuItem& mi) twi = new QTableWidgetItem(text); table->setItem(row, 4, twi); table->item(row, 4)->setFlags(Qt::ItemIsEnabled); - WidenColumn(4, fm.horizontalAdvance(text)); + WidenColumn(4, fm.QFM_WIDTH(text)); if ((int) mi.color != -1) { twi->setForeground(colors[mi.color].q); diff --git a/win/Qt/qt_menu.h b/win/Qt/qt_menu.h index 77dd080f9..9528186e8 100644 --- a/win/Qt/qt_menu.h +++ b/win/Qt/qt_menu.h @@ -24,7 +24,7 @@ public: int width = 0; QFontMetrics fm(font()); for (int i = 0; i < count(); i++) { - int lwidth = fm.horizontalAdvance(item(i)->text()); + int lwidth = fm.QFM_WIDTH(item(i)->text()); width = std::max(width, lwidth); } return width; diff --git a/win/Qt/qt_pre.h b/win/Qt/qt_pre.h index 70f30a8fc..53f7e4718 100644 --- a/win/Qt/qt_pre.h +++ b/win/Qt/qt_pre.h @@ -27,5 +27,14 @@ #pragma GCC diagnostic ignored "-Wshadow" #endif +#include + +/* QFontMetrics::width was deprecated in Qt 5.11 */ +#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) +#define QFM_WIDTH(foo) width(foo) +#else +#define QFM_WIDTH(foo) horizontalAdvance(foo) +#endif + /*qt_pre.h*/ diff --git a/win/Qt/qt_streq.cpp b/win/Qt/qt_streq.cpp index cd1de46e5..dc6712a4d 100644 --- a/win/Qt/qt_streq.cpp +++ b/win/Qt/qt_streq.cpp @@ -80,8 +80,8 @@ bool NetHackQtStringRequestor::Get(char *buffer, int maxchar, int minchar) input.setMaxLength(maxchar - 1); const QString &txt = prompt.text(); - int pw = fontMetrics().horizontalAdvance(txt), - ww = minchar * input.fontMetrics().horizontalAdvance(QChar('X')); + int pw = fontMetrics().QFM_WIDTH(txt), + ww = minchar * input.fontMetrics().QFM_WIDTH(QChar('X')); int heightfactor = ((txt.size() > 16) ? 3 : 2) * 2; // 2 or 3 lines high int widthfudge = (((txt.size() > 16) ? 1 : 2) * 5) * 2; // 5: margn, guttr resize(pw + ww + widthfudge, fontMetrics().height() * heightfactor); diff --git a/win/Qt/qt_xcmd.cpp b/win/Qt/qt_xcmd.cpp index 0d1ad986c..f5ddabeb8 100644 --- a/win/Qt/qt_xcmd.cpp +++ b/win/Qt/qt_xcmd.cpp @@ -244,7 +244,7 @@ NetHackQtExtCmdRequestor::NetHackQtExtCmdRequestor(QWidget *parent) : for (i = 0; extcmdlist[i].ef_txt; ++i) { if (interesting_command(i, set)) { ++ncmds; - butw = std::max(butw, 30 + fm.horizontalAdvance(extcmdlist[i].ef_txt)); + butw = std::max(butw, 30 + fm.QFM_WIDTH(extcmdlist[i].ef_txt)); } } // if any of the choice buttons were bigger than the control buttons, diff --git a/win/Qt/qt_yndlg.cpp b/win/Qt/qt_yndlg.cpp index c6a0ac3d1..0e607a0b6 100644 --- a/win/Qt/qt_yndlg.cpp +++ b/win/Qt/qt_yndlg.cpp @@ -350,7 +350,7 @@ char NetHackQtYnDialog::Exec() QPushButton cancel("Dismiss",this); label.setFrameStyle(QFrame::Box|QFrame::Sunken); label.setAlignment(Qt::AlignCenter); - label.resize(fontMetrics().horizontalAdvance(qlabel)+60,30+fontMetrics().height()); + label.resize(fontMetrics().QFM_WIDTH(qlabel)+60,30+fontMetrics().height()); cancel.move(width()/2-cancel.width()/2,label.geometry().bottom()+8); connect(&cancel,SIGNAL(clicked()),this,SLOT(reject())); centerOnMain(this);