-1

I think my problem is simple to solve, but due to my lack of knowledge, I can't find the answer.

I have this structure :

struct variableOutil {
    std::string Nom;   
    float Valeur;   
    std::string Module;    
    std::string Unite;    
    std::string Format;    
    std::string Accesibilite;    
    std::string Description ;
};

And the problem come from here :

struct variableOutil ToleranceTensionAvion;
ToleranceTensionAvion.Nom = "ToleranceTensionAvion";
QTableWidgetItem* newItem = new QTableWidgetItem();
newItem->setText(ToleranceTensionAvion.Nom);
this->ui->variableTableWidget->setItem(1,0,newItem);

I've got this error :

No matching function for call to 'QTableWidgetItem::setText(std::string&)

The problem is that setText need a const QString &text and tell me that I put in parameter a std::string& , I don't understand why does the type don't match, and what's the difference, after all, this is a simple String.

Thank you.

3

4 Answers 4

3

You should convert std::string to QString by :

newItem->setText(QString::fromStdString(ToleranceTensionAvion.Nom));

Or

newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
2

Try:

newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());

QString allows only Unicode. std::string just stores the bytes and does not work with encodings. The best way to store your texts would probably be UTF-8 encoding.

1

They are completely different classes, and std::stringcan not be used in place of QString. You can build QString fromstd::string and pass it.

QString s =QString::fromStdString(ToleranceTensionAvion.Nom);
newItem->setText(s);
1

just use c._str() on your std::string and it will work for anything that expects a QString

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.