advseo32
advseo32

Reputation: 401

How can I concatenate QStrings?

I have a problem with printing in Qt.

I have HTML code in QString variables. In this code I want to insert data from a database.

I get an error:

E:\apprendreQt\gestionstock6\vente.cpp:117: error: invalid operands of types 
  'const char*' and 'const char [27]' to binary 'operator+'

How can I fix this?

Here is my code:

int num_bl = ui->numeroBLlineEdit->text().toInt() ;
QString html;
QString requette = "select num_facture,date,nom,prenom,code_fiscale,designation,qte_out, prix,(qte_out * prix ) as Montant, sum(qte_out * prix) as Total from ventes join produits_en_ventes join clients  join produits on ventes.vente_id = produits_en_ventes.vente_id and ventes.client_id = clients.client_id and produits_en_ventes.produit_id = produits.produit_id where ventes.client_id = :client_id ";

if(!m_db->isOpen())
    QMessageBox::critical(this,tr("Inventoria Solution"),m_db->lastError().text()) ;
else{
    m_query->clear();
    m_query->prepare(requette);
    m_query->bindValue(":client_id ", num_bl);

    if(!m_query->exec())
        QMessageBox::critical(this,tr("Inventoria Solution"),m_query->lastError().text()) ;
    else{
        html += "       <table>"
                "<thead>"
                "<tr>"
                "<th>N°</th>"
                "<th>Désignation</th>"
                "<th>Qte</th>"
                "<th>Prix Unitaire</th>"
                "<th>Montant</th>"
                "   </tr>"
                "</thead>";
        while(m_query->next())
        {
            int num_article = 1;

            html += "<tr> <td>" + num_article + "</td> <td>"+m_query->value(5).toString()+"</td> <td>"+m_query->value(6).toInt() + "</td> <td>"+m_query->value(7).toInt() + "</td> <td>" + m_query->value(8).toInt() + "</td></tr>";
            num_article++;

        }
            html += "<tfoot>"
                "<tr>"
                "<td>Total:"+ m_query->value(9).toInt()+"</td>"
                "</tr>"
                "</tfoot>"
                "</table>";
    }
    print_Html(html);


}

Upvotes: 23

Views: 93830

Answers (3)

ratchet freak
ratchet freak

Reputation: 48196

in Qt5 you can use the QStringLiteral macro for each string that doesn't need to be localized to transform all the string literals from const char* (the C++ default) into QString, this will also make creation of those QStrings cheaper (on compilers that support it)

for Qt4 you can use the QString(const char*) constructor or QString::fromAscii(const char*) static function

Upvotes: 4

Abhishek Bansal
Abhishek Bansal

Reputation: 12715

I'm not sure about your error. However, AFAIK a Qstring cannot be concatenated with an int as such.

int myInt = 0;
QString text = "someString" + myInt; // WRONG

int myInt = 0;
QString text = "someString" + QString::number( myInt ); // CORRECT

or

int myInt = 0;
QString text = "someString" % QString::number( myInt ); // CORRECT

Upvotes: 29

vahancho
vahancho

Reputation: 21230

If you use operator+, you need to provide QString as an argument, but you use integer values instead: html += "<tr> <td>" + num_article, where num_article is declared as integer. You can replace it with, for example: QString::number(num_article). The same in this line:

"<td>Total:"+ m_query->value(9).toInt()+"</td>"

should be replaced with

"<td>Total:"+ m_query->value(9).toString()+"</td>"

Upvotes: 16

Related Questions