Qt: Fix Qt5 deprecation warnings

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.
This commit is contained in:
Pasi Kallinen
2021-08-14 21:03:13 +03:00
parent 1b3b7b0b29
commit 6554500593
13 changed files with 42 additions and 47 deletions

View 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);
}
}

View 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;

View 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;

View 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];
}

View 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.)");

View 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;

View 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'

View 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;

View 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);
}
}

View 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;

View 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);

View 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,

View 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);