]> granicus.if.org Git - nethack/commitdiff
Qt: Fix Qt5 deprecation warnings
authorPasi Kallinen <paxed@alt.org>
Sat, 14 Aug 2021 18:03:13 +0000 (21:03 +0300)
committerPasi Kallinen <paxed@alt.org>
Sat, 14 Aug 2021 18:08:12 +0000 (21:08 +0300)
Mostly the warnings were about QString::sprintf and QFontMetrics::width.
sprintf replacement is asprintf, which annoyingly behaves differently
from sprintf - it seems to append to the string.

Not thoroughly tested, but seems to work.

13 files changed:
win/Qt/qt_bind.cpp
win/Qt/qt_glyph.cpp
win/Qt/qt_icon.cpp
win/Qt/qt_key.cpp
win/Qt/qt_main.cpp
win/Qt/qt_map.cpp
win/Qt/qt_menu.cpp
win/Qt/qt_menu.h
win/Qt/qt_rip.cpp
win/Qt/qt_stat.cpp
win/Qt/qt_streq.cpp
win/Qt/qt_xcmd.cpp
win/Qt/qt_yndlg.cpp

index f5ed6eb0799edbdb4607e4fd8c5cebc1960f7971..c04e9f30352275ff2ac5e3204bfc3e6bf3b57e3a 100644 (file)
@@ -106,7 +106,7 @@ NetHackQtBind::NetHackQtBind(int& argc, char** argv) :
        splash->repaint();
        lsplash->repaint();
        capt->repaint();
-       qApp->flush();
+       qApp->processEvents();
 
     } else {
        splash = 0;
@@ -156,7 +156,6 @@ void NetHackQtBind::qt_init_nhwindows(int* argc, char** argv)
     seteuid(getuid());
 #endif
 
-    QApplication::setColorSpec(ManyColor);
     instance=new NetHackQtBind(*argc,argv);
 
 #ifdef UNIX
@@ -416,8 +415,7 @@ void NetHackQtBind::qt_display_file(const char *filename, boolean must_exist)
     }
 
     if (complain) {
-       QString message;
-       message.sprintf("File not found: %s\n",filename);
+       QString message = QString::asprintf("File not found: %s\n",filename);
        QMessageBox::warning(NULL, "File Error", message, QMessageBox::Ignore);
     }
 }
index f807282f3d0b8fd34293aedc96ecfd18f870145a..c3657324db6f8ccf1e9d8eea0fc81a330c186d8e 100644 (file)
@@ -50,8 +50,7 @@ NetHackQtGlyphs::NetHackQtGlyphs()
 
        tile_file = PIXMAPDIR "/x11tiles";
        if (!img.load(tile_file)) {
-           QString msg;
-            msg.sprintf("Cannot load 'nhtiles.bmp' or 'x11tiles'.");
+           QString msg = QString::asprintf("Cannot load 'nhtiles.bmp' or 'x11tiles'.");
             QMessageBox::warning(0, "IO Error", msg);
             iflags.wc_ascii_map = 1;
             iflags.wc_tiled_map = 0;
index b8f0e694d28827d0367eb22804999bf83c6a6e16..fea5747421fb85b3d5d95cfa891ae5f385fb801d 100644 (file)
@@ -106,7 +106,7 @@ void NetHackQtLabelledIcon::setLabel(const QString& t, long v, long cv,
     if (v==NoNum) {
        buf = "";
     } else {
-       buf.sprintf("%ld", v);
+       buf = QString::asprintf("%ld", v);
     }
     setLabel(t + buf + tail, cv < prev_value);
     prev_value=cv;
index 0fe9595458205aa96dffaf874b0870ebe41fa25e..7b0e63aac69c4a466d658a98641b3327fdd06198 100644 (file)
@@ -94,7 +94,7 @@ int NetHackQtKeyBuffer::GetAscii()
 
 Qt::KeyboardModifiers NetHackQtKeyBuffer::GetState()
 {
-    if ( Empty() ) return 0;
+    if ( Empty() ) return Qt::NoModifier;
     Qt::KeyboardModifiers r=TopState();
     out=(out+1)%maxkey;
     return r;
@@ -114,7 +114,7 @@ int NetHackQtKeyBuffer::TopAscii() const
 
 Qt::KeyboardModifiers NetHackQtKeyBuffer::TopState() const
 {
-    if ( Empty() ) return 0;
+    if ( Empty() ) return Qt::NoModifier;
     return state[out];
 }
 
index d0fbd003617cdd3268cc9386dfb8a822ec8f2f58..21f6b265717e3dd6e6baa9dcc7e93f27288dd18d 100644 (file)
@@ -458,8 +458,7 @@ aboutMsg()
         *p = '\0';
     /* it's also long; break it into two pieces */
     (void) strsubst(vbuf, " - ", "\n- ");
-    QString msg;
-    msg.sprintf(
+    QString msg = QString::asprintf(
         // format
         "NetHack-Qt is a version of NetHack built using" // no newline
 #ifdef KDE
@@ -1022,8 +1021,7 @@ void NetHackQtMainWindow::doQuit(bool)
     // nethack's #quit command itself) but this routine is unconditional
     // in case someone wants to change that
 #ifdef MACOS
-    QString info;
-    info.sprintf("This will end your NetHack session.%s",
+    QString info = QString::asprintf("This will end your NetHack session.%s",
                  !g.program_state.something_worth_saving ? ""
                  : "\n(Cancel quitting and use the Save command"
                    "\nto save your current game.)");
index 90320e5eb111080a498c3ec607628ba988fb7ce7..344899425e4d39a12f8deed1141c64911ccfc755 100644 (file)
@@ -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.width("M") > qt_settings->glyphs().width())
+        if (fm.horizontalAdvance("M") > qt_settings->glyphs().width())
             break;
         if (fm.height() > qt_settings->glyphs().height())
             break;
index b33058ae55979da2d51d9f8f10e4055ccb1c51c3..860ac764cd665f0842f5dc015131be7cd9dc9cc1 100644 (file)
@@ -196,7 +196,7 @@ NetHackQtMenuWindow::NetHackQtMenuWindow(QWidget *parent) :
 
     QPoint pos(0,ok->height());
     move(pos);
-    prompt.setParent(this,0);
+    prompt.setParent(this);
     prompt.move(pos);
 
     grid->addWidget(ok, 0, 0);
@@ -378,8 +378,8 @@ void NetHackQtMenuWindow::PadMenuColumns(bool split_descr)
     QFontMetrics fm(table->font());
     QString col0width_str = "";
     if (biggestcount > 0L)
-        col0width_str.sprintf("%*ld", std::max(countdigits, 1), biggestcount);
-    int col0width_int = (int) fm.width(col0width_str) + MENU_WIDTH_SLOP;
+        col0width_str = QString::asprintf("%*ld", std::max(countdigits, 1), biggestcount);
+    int col0width_int = (int) fm.horizontalAdvance(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.width(columns[fld] + (lastcol ? "" : "  "));
+                int w = fm.horizontalAdvance(columns[fld] + (lastcol ? "" : "  "));
                 if (fld >= (int) col_widths.size()) {
                     col_widths.push_back(w); // add another element
                 } else if (col_widths[fld] < w) {
@@ -421,7 +421,7 @@ void NetHackQtMenuWindow::PadMenuColumns(bool split_descr)
             QString Amt = "";
             long amt = count(row); // fetch item(row,0) as a number
             if (amt > 0L)
-                Amt.sprintf("%*ld", countdigits, amt);
+                Amt = QString::asprintf("%*ld", countdigits, amt);
             cnt->setText(Amt);
         }
 
@@ -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.width(columns[fld]) < width)
+                while (fm.horizontalAdvance(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.width(text) + MENU_WIDTH_SLOP;
+        int wid = fm.horizontalAdvance(text) + MENU_WIDTH_SLOP;
         if (wid > widest4)
             widest4 = wid;
     }
@@ -464,7 +464,7 @@ void NetHackQtMenuWindow::UpdateCountColumn(long newcount)
     } else {
         biggestcount = std::max(biggestcount, newcount);
         QString num;
-        num.sprintf("%*ld", std::max(countdigits, 1), biggestcount);
+        num = QString::asprintf("%*ld", std::max(countdigits, 1), biggestcount);
         int numlen = (int) num.length();
         if (numlen % 2)
             ++numlen;
@@ -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.width("999999") + MENU_WIDTH_SLOP);
+        WidenColumn(0, fm.horizontalAdvance("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.width(letter);
+    int w = (int) fm.horizontalAdvance(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.width(text));
+    WidenColumn(4, fm.horizontalAdvance(text));
 
     if ((int) mi.color != -1) {
        twi->setForeground(colors[mi.color].q);
@@ -875,7 +875,7 @@ void NetHackQtMenuWindow::ToggleSelect(int row, bool already_toggled)
                 amt = count(row); // fetch item(row,0) as a number
                 QString Amt = "";
                 if (amt > 0L)
-                    Amt.sprintf("%*ld", countdigits, amt);
+                    Amt = QString::asprintf("%*ld", countdigits, amt);
                 countfield->setText(Amt); // store right-justified value
             }
             ClearCount(); // blank out 'countstr' and reset 'counting'
index e69265a4f42bfe4c32e93343cd260be0b16d4946..77dd080f9921b364f9766812c51eac8556899e7e 100644 (file)
@@ -24,7 +24,7 @@ public:
        int width = 0;
        QFontMetrics fm(font());
        for (int i = 0; i < count(); i++) {
-           int lwidth = fm.width(item(i)->text());
+           int lwidth = fm.horizontalAdvance(item(i)->text());
            width = std::max(width, lwidth);
        }
        return width;
index aa7a8ebc42472c90f7f03f2cbda20e84c303d8fe..b4aeb1a3a329533892254a07b34240a8dbc2a135 100644 (file)
@@ -36,7 +36,7 @@ tryload(QPixmap& pm, const char* fn)
 {
     if (!pm.load(fn)) {
        QString msg;
-       msg.sprintf("Cannot load \"%s\"", fn);
+       msg = QString::asprintf("Cannot load \"%s\"", fn);
        QMessageBox::warning(NetHackQtBind::mainWidget(), "IO Error", msg);
     }
 }
index edbda992ef2acd612dd37d1c914baec339c66739..f94079c29c5e278cb0e3fee4def72dade90c637d 100644 (file)
@@ -622,7 +622,7 @@ void NetHackQtStatusWindow::HitpointBar()
             geoH.setRight(std::min(lox + pxl_health - 1, hix));
             hpbar_health.setGeometry(geoH);
             w = geoH.right() - geoH.left() + 1; // might yield 0 (ie, if dead)
-            styleH.sprintf(styleformat, barcolors[colorindx][0], w, w);
+            styleH = QString::asprintf(styleformat, barcolors[colorindx][0], w, w);
             hpbar_health.setStyleSheet(styleH);
             // when healing, having the old injury-side shown while the new
             // health-side expands pushes the injury farther right and it's
@@ -635,7 +635,7 @@ void NetHackQtStatusWindow::HitpointBar()
             geoI.setRight(hix);
             hpbar_injury.setGeometry(geoI);
             w = geoI.right() - geoI.left() + 1;
-            styleI.sprintf(styleformat, barcolors[colorindx][1], w, w);
+            styleI = QString::asprintf(styleformat, barcolors[colorindx][1], w, w);
             hpbar_injury.setStyleSheet(styleI);
             if (geoI.left() != oldleft)
                 hpbar_injury.move(geoI.left(), geoI.top());
@@ -653,7 +653,7 @@ void NetHackQtStatusWindow::HitpointBar()
             geoH.setRight(hix);
             hpbar_health.setGeometry(geoH);
             w = geoH.right() - geoH.left() + 1;
-            styleH.sprintf(styleformat, barcolors[colorindx][0], w, w);
+            styleH = QString::asprintf(styleformat, barcolors[colorindx][0], w, w);
             hpbar_health.setStyleSheet(styleH);
             hpbar_health.show();
 
@@ -704,13 +704,13 @@ void NetHackQtStatusWindow::updateStats()
 
     int st = ACURR(A_STR);
     if (st > STR18(100)) {
-        buf.sprintf("Str:%d", st - 100);        // 19..25
+        buf = QString::asprintf("Str:%d", st - 100);        // 19..25
     } else if (st == STR18(100)) {
-        buf.sprintf("Str:18/**");               // 18/100
+        buf = QString::asprintf("Str:18/**");               // 18/100
     } else if (st > 18) {
-        buf.sprintf("Str:18/%02d", st - 18);    // 18/01..18/99
+        buf = QString::asprintf("Str:18/%02d", st - 18);    // 18/01..18/99
     } else {
-        buf.sprintf("Str:%d", st);              //  3..18
+        buf = QString::asprintf("Str:%d", st);              //  3..18
     }
     str.setLabel(buf, NetHackQtLabelledIcon::NoNum, (long) st);
     dex.setLabel("Dex:", (long) ACURR(A_DEX));
@@ -799,7 +799,7 @@ void NetHackQtStatusWindow::updateStats()
     }
     QString buf2;
     char buf3[BUFSZ];
-    buf2.sprintf("%s the %s", upstart(strcpy(buf3, g.plname)),
+    buf2 = QString::asprintf("%s the %s", upstart(strcpy(buf3, g.plname)),
                  buf.toLatin1().constData());
     name.setLabel(buf2, NetHackQtLabelledIcon::NoNum, u.ulevel);
 
@@ -818,13 +818,13 @@ void NetHackQtStatusWindow::updateStats()
         level.setCompareMode(NeitherIsBetter);
     if (Upolyd) {
         // You're a monster!
-        buf.sprintf("/%d", u.mhmax);
+        buf = QString::asprintf("/%d", u.mhmax);
         hp.setLabel("HP:", std::max((long) u.mh, 0L), buf);
         level.setLabel("HD:", (long) mons[u.umonnum].mlevel); // hit dice
         // Exp points are not shown when HD is displayed instead of Xp level
     } else {
         // You're normal.
-        buf.sprintf("/%d", u.uhpmax);
+        buf = QString::asprintf("/%d", u.uhpmax);
         hp.setLabel("HP:", std::max((long) u.uhp, 0L), buf);
         // if Exp points are to be displayed, append them to Xp level;
         // up/down highlighting becomes tricky--don't try very hard;
@@ -835,9 +835,9 @@ void NetHackQtStatusWindow::updateStats()
         for (int i = ::flags.showexp ? 0 : 3; i < 4; ++i) {
             // passes 0,1,2 are with Exp, 3 is without Exp and always fits
             if (i < 3) {
-                buf.sprintf("%s%ld/%ld", lvllbl[i], (long) u.ulevel, u.uexp);
+                buf = QString::asprintf("%s%ld/%ld", lvllbl[i], (long) u.ulevel, u.uexp);
             } else {
-                buf.sprintf("%s%ld", lvllbl[i - 3], (long) u.ulevel);
+                buf = QString::asprintf("%s%ld", lvllbl[i - 3], (long) u.ulevel);
             }
             // +2: allow a couple of pixels at either end to be clipped off
             if (fm.size(0, buf).width() <= (2 + level.label->width() + 2))
@@ -857,7 +857,7 @@ void NetHackQtStatusWindow::updateStats()
     was_polyd = Upolyd ? true : false;
     had_exp = (::flags.showexp && !was_polyd) ? true : false;
 
-    buf.sprintf("/%d", u.uenmax);
+    buf = QString::asprintf("/%d", u.uenmax);
     power.setLabel("Pow:", (long) u.uen, buf);
     ac.setLabel("AC:", (long) u.uac);
     // gold prefix used to be "Au:", tty uses "$:"; never too wide to fit;
@@ -917,7 +917,7 @@ void NetHackQtStatusWindow::updateStats()
             static const char *const scrlbl[3] = { "Score:", "Scr:", "S:" };
             QFontMetrics fm(score.label->font());
             for (int i = 0; i < 3; ++i) {
-                buf.sprintf("%s%ld", scrlbl[i], pts);
+                buf = QString::asprintf("%s%ld", scrlbl[i], pts);
                 // +2: allow couple of pixels at either end to be clipped off
                 if (fm.size(0, buf).width() <= (2 + score.width() + 2))
                     break;
index 04fed5ca9cd9d9251aaa177a1f635dd2d108d1c4..cd1de46e59ee538f0bc4fecaafdba139f5316862 100644 (file)
@@ -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().width(txt),
-        ww = minchar * input.fontMetrics().width(QChar('X'));
+    int pw = fontMetrics().horizontalAdvance(txt),
+        ww = minchar * input.fontMetrics().horizontalAdvance(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);
index 26e19edfbc18b4413f7d685e16d3ff406504f48d..0d1ad986c212fb16cc0de3afa2e2621dff6a70c3 100644 (file)
@@ -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.width(extcmdlist[i].ef_txt));
+            butw = std::max(butw, 30 + fm.horizontalAdvance(extcmdlist[i].ef_txt));
         }
     }
     // if any of the choice buttons were bigger than the control buttons,
index 5a7bad1c11cecc69df31c872ebbcd3a6030cd1f4..c6a0ac3d123ef642ad5cfb7884c470e47d8e0713 100644 (file)
@@ -148,7 +148,7 @@ char NetHackQtYnDialog::Exec()
            q->setMargin(4);
            vb->addWidget(q);
        }
-       QGroupBox *group = new QGroupBox(bigq ? QString::null : qlabel, this);
+       QGroupBox *group = new QGroupBox(bigq ? QString() : qlabel, this);
        vb->addWidget(group);
        QHBoxLayout *groupbox = new QHBoxLayout();
        group->setLayout(groupbox);
@@ -350,7 +350,7 @@ char NetHackQtYnDialog::Exec()
        QPushButton cancel("Dismiss",this);
        label.setFrameStyle(QFrame::Box|QFrame::Sunken);
        label.setAlignment(Qt::AlignCenter);
-       label.resize(fontMetrics().width(qlabel)+60,30+fontMetrics().height());
+       label.resize(fontMetrics().horizontalAdvance(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);