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

C++ 如何在C+;中将自定义文本设置为QLabel+;?,c++,c++11,qt5,qlabel,C++,C++11,Qt5,Qlabel,我正在开发一个C++/Qt模拟器,它集成了一个参数的页面。在参数末尾,一个QLabel通知用户输入的数据是否有效。 此文本应以自定义颜色显示,因此我实现了以下功能: 参数Dialog.h #include <iostream> #include <QtWidgets> using namespace std; class ParametersDialog: public QDialog { Q_OBJECT public: Param

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

参数Dialog.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);
};
#包括
#包括
使用名称空间std;
类参数对话框:公共QDialog{
Q_对象
公众:
参数dialog(QWidget*parent=nullptr);
~ParametersDialog();
...
私人:
QLabel*notificationLabel=新的QLabel;
...
作废通知(字符串消息、字符串颜色);
};
参数Dialog.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>");
}
#包括“/参数dialog.h”
ParametersDialog::ParametersDialog(QWidget*parent):QDialog(parent){
...
通知(“测试1 2”、“绿色”);
}
...
void参数dialog::notify(字符串消息,字符串颜色=“红色”){
notificationLabel->setText(“+消息+”);
}
我不明白为什么它会给我这个错误:

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>");
                                                                               ^
D:\dev\\u MyCode\SM\u拖缆\\参数dialog.cpp:65:79:错误:调用'QLabel::setText(std::\uuuucxx11::basic\u string')时没有匹配的函数
notificationLabel->setText(“+消息+”);
^
我知道我的字符串连接创建了一个不能设置为
QLabel
文本的
basic_string
元素


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

问题是std::string和QString不是直接可连接的

可以做一个技巧:

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

问题在于std::string和QString不是直接可连接的

可以做一个技巧:

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

a
basic_string
只是
std::string
但是该方法采用
QString
我尝试过这个解决方案,但它给了我一个类似的错误:
notificationLabel->setText(QString(“+message+”)这是否回答了您的问题?是的,它是有效的,我忘记了
QString
不能像我那样构建:)非常感谢
basic_string
只是
std::string
但是这个方法需要
QString
我尝试过这个解决方案,但它给了我一个类似的错误:
notificationLabel->setText(QString(“+message+”)这是否回答了您的问题?是的,它可以工作,我忘记了
QString
不能像我那样构建:)非常感谢