如何使QString在QT中具有特定大小?

时间:2019-04-17 14:48:30

标签: c++ qt c++11

我的问题是,我将拥有一个QString val,在那里将获得不同的值。但是我希望所有结果都具有固定数量的字符,例如20。

因此,如果我输入的单词少于该字符的数量,它将在右边添加空格。

一个例子,假设我有2个单词:"abc""abcdefghijklmno"

first case

qDebug()<<val; //"abc "

second case

qDebug()<<val; //"abcdefghijklmno "

怎么办?

3 个答案:

答案 0 :(得分:3)

使用可以使用S2。它需要大小和一个填充字符:

QString::resize

答案 1 :(得分:1)

您可以使用以下方法

qDebug() << QString("%1").arg(yourStringVar, fieldWidth, QChar('fillEmptySpaceChar'));

在您的示例中可能是

qDebug() << QString("%1").arg(val, 32, QChar(' '));

要使填充字符从右开始填充,只需使用以下

qDebug() << QString("%1").arg(val, -32, QChar(' '));

答案 2 :(得分:1)

或者您可以使用为此专门创建的功能QString::leftJustified() / app.use( session({ name: SESSION_NAME, secret: SESSION_SECRET, resave: false, saveUninitialized: false, cookie: { maxAge: parseInt(SESSION_LIFETIME, 10), // 24H in config file httpOnly: true, sameSite: true, secure: !IN_DEV } }) ); googleRouter.get("/oauthcallback", async (req, res) => { try { if (req.query.code && req.query.code !== "") { const oauth2Client = initGoogleAuth(); const tokens = await getGoogleToken(oauth2Client, req.query.code); if (tokens && tokens !== "") { const s = await saveTokensInSession(req.session, tokens); oauth2Client.setCredentials(tokens); // save data in session req.session.oauth2Client = oauth2Client; req.session.save(); // oauth2Client is well saved here in session console.log(req.session.oauth2Client); //redirect to the next url res.redirect("/googleauth/test"); } else res.send("error while getting tokens"); } else res.send("error while getting authorization code"); } catch (error) { res.send(`error while getting token : ${error}`); } }); googleRouter.get("/test", async (req, res) => { // oauth2Client is empty here and sessionID is different console.log(req.session.oauth2Client); });

QString::rightJustified()

如果输入的字符串长于固定宽度,这也使您可以选择继续操作的方式。