Problem on changing QSqlRetionalTableModel result column value.
-
Hi there,
I have a problem with changing QSqlRetionalTableModel result column value. Here is my code:
enum { CATEGORY_ID = 0, CREATED_DATE = 1, AMOUNT = 2, EXPENSE_ID = 3, REMARKS = 4 }; Add_Transaction_Record_Window::Add_Transaction_Record_Window(QWidget *parent, FINANCIAL_DATABASE *database) : QMainWindow(parent), m_add_trans_record(new Ui::Add_Transaction_Record_Window) { m_add_trans_record->setupUi(this); m_financial_database = database; m_Table_Model = new QSqlRelationalTableModel(this, m_financial_database->get_database()); m_Table_Model->setTable("public.expense_details"); m_Table_Model->setRelation(CATEGORY_ID, QSqlRelation("public.categories", "category_id", "category_type")); m_Table_Model->setSort(CREATED_DATE, Qt::AscendingOrder); m_Table_Model->setHeaderData(EXPENSE_ID, Qt::Horizontal, tr("Expense ID")); m_Table_Model->setHeaderData(CREATED_DATE, Qt::Horizontal, tr("Created Date")); m_Table_Model->setHeaderData(CATEGORY_ID, Qt::Horizontal, tr("Category")); m_Table_Model->setHeaderData(AMOUNT, Qt::Horizontal, tr("Amount")); m_Table_Model->setHeaderData(REMARKS, Qt::Horizontal, tr("Remarks")); m_Table_Model->select(); long amount_in_long; unsigned int no_of_rows = m_Table_Model->rowCount(); QString amount_in_string; QModelIndex index; for (unsigned int counter = 0; counter < no_of_rows; ++counter) { index = m_Table_Model->index(counter, AMOUNT); ``` amount_in_long = index.data().toULongLong(); amount_in_string = QString("%1").arg(static_cast<float> (amount_in_long) / 100, 0, 'f', 2); m_Table_Model->setData(index, amount_in_string, Qt::EditRole); amount_in_string = index.data().toString(); } }
In the table record, the amount has no decimal point. My above code is converting the amount to have a decimal point by dividing 100.
When i run, it displays:amount_in_long = index.data().toULongLong(); amount_in_string = QString("%1").arg(static_cast<float> (amount_in_long) / 100, 0, 'f', 2); m_Table_Model->setData(index, amount_in_string, Qt::EditRole); amount_in_string = index.data().toString();
Under debugging mode, when counter = 100, the value result as follow:
amount_in_long = 100
amount_in_string = 1.00
after executing setData function call, the amount_in_string is 100. Does anyone know why it changes from 1.00 to 100? -
Hi,
You are using the wrong tool. You should not try to change the data, it's only a viewing thing. You should rather implement a custom QStyledItemDelegate and implement the displayText method. In there do your transformation.