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

Problem with QTableWidget item or cell.



  • using Fedora19 and Qt4.8 with QtCreator.
    I am putting floating point number into QTableWidget cells. Some of the floats are displayed in the cells with an exponent which is not a problem for viewing. When I read back those cells, convert to floats and try to perform math operations, the floating point numbers are not representative of what the string in the cell says. For example 5.36767e-05 comes out as 5.36767 not .0000536767. Is there a way to keep the QTableWidget from displaying in scientific notation, or a way to get the float out of the cell correctly.
    Thanks in advance



  • @emp1953
    In addition to @jsulm. Do you have to use a QTableWidget for this? I believe (subject to correction by an expert?) that QTableWidgetItems store items as text. All that converting to/from text, possible precision losses.... Would a QTableView with a model explicitly storing doubles be a better approach?


  • Lifetime Qt Champion

    @emp1953 said in Problem with QTableWidget item or cell.:

    When I read back those cells

    Please show how you are doing this



  • @emp1953
    In addition to @jsulm. Do you have to use a QTableWidget for this? I believe (subject to correction by an expert?) that QTableWidgetItems store items as text. All that converting to/from text, possible precision losses.... Would a QTableView with a model explicitly storing doubles be a better approach?



  • I inherited this job due to a developer got covid and died. I suppose that I could refactor it and try a qtableview. It would be a big effort though just because I can't get the float to go into the qtablewidget cell as a floating point number without an exponent.



  • for(int i = 0; i < max_i; i++)
        {
            bool ok;
            float tmppct = 0.0;
            float thisbatch = ui->le_req_sum->text().toFloat();
    
    
    
            // get the grams
            if(i==21)
            {
                qDebug() << "???";
            }
            float tmpgrams = ui->tableWidget->item(i, 2)->text().toFloat(&ok);
            // bet the units
            float tmpunits = ui->tableWidget->item(i, 1)->text().toFloat(&ok);
            // divide grams by units
            float tmpsum;
            //float tmpsum = tmpgrams/tmpunits;
            localvector.push_back(tmpsum);
            // load the interimvector
            std::ostringstream ss;
            //ss << std::fixed << (tmpsum);
            ss << tmpsum;
            std::string s(ss.str());
            interimvector.push_back(s);
    
        }
    

  • Lifetime Qt Champion

    Hi,

    You can use a custom QStyledItemDelegate to show the data however you want.

    Then you can store the original value as user data in the item and then use that one when retrieving it for further processing.



  • Thanks for the advice. I have a large QTableWidget on each one of 14 tabs. So it looks like I have a large refactoring effort in front of me.


  • Lifetime Qt Champion

    If refactoring, then consider the proposition from @JonB using QTableView and a custom model.


Log in to reply