【问题标题】:How to display concatenated string and int in qlineedit or qtextedit? [duplicate]如何在 qlineedit 或 qtextedit 中显示串联的字符串和 int? [复制]
【发布时间】:2020-02-26 15:19:07
【问题描述】:

我在这里:

int num = 000;
std::string result;
std::string text = "asdasd";
result += text + std::to_string(num);

我想在 qlineedit 中显示结果。

【问题讨论】:

    标签: c++ qlineedit


    【解决方案1】:

    由于您使用的是,因此您应该使用QString 而不是std::string,以避免许多不必要的转换。
    事实上,QString 是所有 Qt 库(容器,...)使用的字符串类。

    您的样本将变为:

    int num = 0;
    QString text = "asdasd";
    QString result = text + QString::number(num);
    

    然后,你就可以写了:

    my_widget->setText(result); // QLineEdit or QTextEdit
    

    如果您确实想使用std::string,则需要将其转换为正确的QString。为此,您可以使用QString::fromStdString()

    例如:

    my_widget->setText(QString::fromStdString(result));
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2015-06-28
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多