23

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


}
Mat
  • 202,337
  • 40
  • 393
  • 406
advseo32
  • 401
  • 1
  • 5
  • 15
  • possible duplicate of [Concatenating two QStrings with an integer](http://stackoverflow.com/questions/7011447/concatenating-two-qstrings-with-an-integer) This one was also already marked for that: http://stackoverflow.com/questions/11223674/concatenate-stl-stringintintint-in-qstring – László Papp Sep 16 '13 at 11:04

3 Answers3

30

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
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
16

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>"
vahancho
  • 20,808
  • 3
  • 47
  • 55
4

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

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • then you can use the `QString(const char*)` constructor or `QString::fromAscii(const char*)` static function – ratchet freak Sep 16 '13 at 11:07
  • That is why your suggestion is bad IMHO... you will need to have porting effort between the two major variants as opposed to the other solutions presented... At the very least, it would be nice to make it clear that this is a limited solution (Qt 5 only). – László Papp Sep 16 '13 at 11:08
  • @LaszloPapp added version info – ratchet freak Sep 16 '13 at 11:14
  • ok, thanks, but I think the whole thread should be closed as duplicate because this is a fairly basic question, and answered in mostly the same manner twice, already. Perhaps you could post the Qt 5 feature in there rather than here. – László Papp Sep 16 '13 at 11:32