如何在 C++ 中将自定义文本设置为 QLabel?

How can I set custom text to a QLabel in C++?

我正在开发集成参数页面的 C++/Qt 模拟器。在参数的末尾,QLabel 通知用户输入的数据是否有效。 此文本应以自定义颜色显示,所以我实现了这个:

ParametersDialog.h

#include <iostream>
#include <QtWidgets>

using namespace std;

class ParametersDialog: public QDialog {
    Q_OBJECT

    public:
        ParametersDialog(QWidget *parent = nullptr);
        ~ParametersDialog();

    ...

    private:
        QLabel *notificationLabel = new QLabel;
        ...
        void notify(string message, string color);
};

ParametersDialog.cpp

#include "<<src_path>>/ParametersDialog.h"

ParametersDialog::ParametersDialog(QWidget *parent): QDialog(parent) {
    ...
    notify("TEST TEST 1 2 1 2", "green");
}

...

void ParametersDialog::notify(string message, string color = "red") {
    notificationLabel->setText("<font color=" + color + ">" + message + "</font>");
}

我不明白为什么会出现这个错误:

D:\dev\_MyCode\SM_Streamer\<<src_path>>\ParametersDialog.cpp:65:79: error: no matching function for call to 'QLabel::setText(std::__cxx11::basic_string<char>)'
  notificationLabel->setText("<font color=" + color + ">" + message + "</font>");
                                                                               ^

我知道我的字符串连接创建了一个无法设置为 QLabel 文本的 basic_string<char> 元素。

我的 notify 方法最简单的实现是什么?

问题是 std::string 和 QString 不能直接连接...

一招可以:

QString mx = "<font color=%1>%2</font>";
notificationLabel->setText(mx.arg(color.c_str(), message.c_str()));