Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem on changing QSqlRetionalTableModel result column value.
Forum Updated to NodeBB v4.3 + New Features

Problem on changing QSqlRetionalTableModel result column value.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 148 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Hon
    wrote on last edited by
    #1

    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:

    Screenshot 2024-01-25 221929.png

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved