mardi 26 mai 2020

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

I'm working on a C++/Qt simulator which integrates a parameter's page. At the end of the parameters, a QLabel notifies the user whether the entered data is valid or not. This text should appear with a custom color, so I implemented this:

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

I don't understand why it gives me this error:

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>");
                                                                               ^

I understand that my string concatenation has created a basic_string<char> element that cannot be set as a QLabel text. what could be the simplest implementation of my notify method?

Aucun commentaire:

Enregistrer un commentaire