Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I concatenate QStrings?

Tags:

printing

qt

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


}
like image 971
advseo32 Avatar asked Sep 16 '13 10:09

advseo32


People also ask

How do I concatenate strings in Qt?

QVector<QString> strings{ . . . }; std::accumulate(strings. cbegin(), strings. cend(), QString{}); The algorithm does what you'd expect in this example – it starts with an empty QString and adds each of the strings from the vector to it, thus creating a concatenated string.

How do you concatenate 2 strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


1 Answers

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
like image 178
Abhishek Bansal Avatar answered Sep 27 '22 20:09

Abhishek Bansal