-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.

Evans Belloeil
  • 2,233
  • 6
  • 37
  • 68

4 Answers4

3

You should convert std::string to QString by :

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

Or

newItem->setText(QString::fromUtf8(ToleranceTensionAvion.Nom.c_str());
Rakib
  • 6,975
  • 6
  • 24
  • 43
Nejat
  • 28,909
  • 10
  • 91
  • 119
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.

Rakib
  • 6,975
  • 6
  • 24
  • 43
Matthias
  • 443
  • 1
  • 6
  • 11
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);
Rakib
  • 6,975
  • 6
  • 24
  • 43
1

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

AngryDuck
  • 3,561
  • 9
  • 50
  • 82