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

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:

    void PUPILS_PAID_SCH_FEES_BY_GRADE::on_VIEW_PUPILS_clicked()
    {
        int sub=ui->comboBox->currentIndex();
    
        QSqlDatabase::database();
        querymodel=new QSqlQueryModel;
        QSqlQuery query;
    
        switch (sub)
        {
        case 0:
    
        querymodel->setQuery("SELECT SURNAME,LASTNAME,GRADE,CLASS,AMOUNT,DATE FROM payments WHERE GRADE=8");
    if(query.size()>0){
    
        ui->tableView->setModel(querymodel);
    }
    else
     {
    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.



  • @GREYON
    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



  • @GREYON
    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