Nominate our 2022 Qt Champions!

qtsqlquerymodel table veiw

  • Hello friends, I have connected may qt application to MySQL database. However I have two issues in which I need some help.

    • First what can be the best data type for the currency(money) in the data base and how can I include the symbol of the currency in the data base record such that even when am displaying my query results in querymodel table veiw,even the symbol for the currency is show instead of a number only?

    • When I use qtsqlququery model to view tables,each row (record) is numbered automatically,is the a way of displaying a table without having the rows numbered?

    • Lastly I have this query:

        int sub=ui->comboBox->currentIndex();
        querymodel=new QSqlQueryModel;
        QSqlQuery query;
        switch (sub)
        case 0:
    QMessageBox::warning(this,"warning", "no one has paid in this class");

    What I want is that if no one has paid in grade 8 classes,nothing should be displayed only the warning message,however what is happening is that even when some one has paid in this class, the table is not shown only the warning message is coming.where iam I going wrong please.I will appreciate your help.

    MySQL does not have a currency type. Nor does C++. Which is a bit of an issue. I used DECIMAL(8, 2), you could increase the first argument if you want larger amounts. The really important thing is that you do not store any kind of currency symbol in the database, and you do not get tempted to store it as string like "123.45". When you want to display the amount only then do you add the symbol, e.g. in a QStyledItemDelegate for a QTableView.

    Don't know about the row numbering, depends what widget you are using to display which you do no not mention.

    Your query code is all over the place. There is no relationship between QSqlQuery query; variable and querymodel->setQuery(...), so query.size() is not going to tell you anything, is probably always 0, and so the else is followed. QSqlDatabase::database(); is a statement which does nothing. Check out the examples in Qt's documentation.

  • @JonB
    Thanks for your help,the widget am using is a model based table view

    what do you by saying your code is all over the place?what is your suggestion? how can I use the if statement successfully?

Log in to reply