Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

invalid operands to binary expression ('const char *' and 'QString')



  • Hi, i'va a problem to compare different type of text..

    if (!aDatabase.open())
            {
            // Messaggio Errore
            QMessageBox::critical(this, "Errore", aDatabase.lastError().text());
            return;
            }
        //COMBO CODICE - DA DATABASE
        QSqlQuery q;
        q.prepare("Select CODICE from ARTICOLI where Cliente == " & ui->comboBox_Cliente->currentText() &  " order by CODICE");
        if (ui->comboBox_Cliente->currentText() == "")
            {
            QMessageBox::warning(this, "Errore", "Selezionare prima il Cliente!");
            return;
            }
        if (q.exec())
            {
                QSqlQueryModel *TModel = new QSqlQueryModel(this);
                TModel->setQuery(q);
                ui->comboBox_Codice->setModel(TModel);
            }
    

    compiling i've this error but idon't know how solve it..

    .../regtemciclo.cpp:81: error: invalid operands to binary expression ('const char *' and 'QString')
    q.prepare("Select CODICE from ARTICOLI where Cliente == " & ui->comboBox_Cliente->currentText() & " order by CODICE");
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    anyone help me please??



  • @TheCipo76

    q.prepare("Select CODICE from ARTICOLI where Cliente == " & ui->comboBox_Cliente->currentText() &  " order by CODICE");
    

    Let's start with: What in the world is that & operator supposed to be? If it's string concatenation, that's + in C++, we're not in Visual Basic here! :)

    Then you might want to look at http://doc.qt.io/qt-5/qstring.html#arg as a neater way of accomplishing it.:

    QString("Select CODICE from ARTICOLI where Cliente == %1 order by CODICE").arg(ui->comboBox_Cliente->currentText())
    

    Up to you, matter of preference.

    Finally, you sure == is the right operator to pass in your query? Last time I looked SQL's operator is =, but you may know better for your SQL provider.



  • ooops.. you're right!!
    Thank you



  • @TheCipo76
    Check what I've added about your SQL == operator?

    Also, if your ui->comboBox_Cliente->currentText() is some literal text (as opposed to a SQL expression), won't you need SQL quote marks around it, like say:

    QString("Select CODICE from ARTICOLI where Cliente == '%1' order by CODICE").arg(ui->comboBox_Cliente->currentText())
    

    And even then it won't work right if the literal string contains a ' character....


Log in to reply