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. lose precesion when qtreeview show double value

lose precesion when qtreeview show double value

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 509 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.
  • W Offline
    W Offline
    WuYaZi
    wrote on last edited by
    #1

    my json contains double value like 32.6743408794, but when i load and show it ,it only shows 32.67。When debug in QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent) fuction , i found the value in child is correct actually, so i think it's a treeview widget problem not jsonmodel. If someone could give me some advice, i'll appreciate. :)
    alt text
    alt text

    1 Reply Last reply
    0
    • Chris KawaC Chris Kawa

      @Christian-Ehrlicher Well yes, but also no :) data() returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpret DisplayRole is "the data to be displayed", not "how to display the data".

      W Offline
      W Offline
      WuYaZi
      wrote on last edited by Chris Kawa
      #8

      @Chris-Kawa thank for ur explaination. after custom my delegate , i solved this problem.alt text

      and when click double value item, should reimplement setEditorData function to specify the format,otherwise the double value will shrink too. below is my key code,hope it helps someone else.

      QWidget* XtrTaskViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      	return QStyledItemDelegate::createEditor(parent, option, index);
      }
      
      void XtrTaskViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
      {
      	if (index.data().type() == QVariant::Double)
      	{
      		QDoubleSpinBox* doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
      
      		doubleSpinBox->setDecimals(10);
      		doubleSpinBox->setValue(index.data().toDouble());
      	}
      	else 
      	{
      		QStyledItemDelegate::setEditorData(editor, index);
      	}
      }
      
      1 Reply Last reply
      0
      • W Offline
        W Offline
        WuYaZi
        wrote on last edited by
        #2

        btw, i use https://github.com/dridk/QJsonModel for my json model ,it's easy to use, but this problem annoy me.

        Christian EhrlicherC 1 Reply Last reply
        0
        • W WuYaZi

          btw, i use https://github.com/dridk/QJsonModel for my json model ,it's easy to use, but this problem annoy me.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Fix your QJsonModel::data() function to return a proper string for Qt::DisplayRole.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          W 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Fix your QJsonModel::data() function to return a proper string for Qt::DisplayRole.

            W Offline
            W Offline
            WuYaZi
            wrote on last edited by
            #4

            @Christian-Ehrlicher
            thank for ur reply.
            but i didn't want to change the type of such numberic value, cause i want to edit them through gui, if i return string , then when i save the json , a stringToDouble function will be added. I just wonder wheather there is a simpler way :

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @WuYaZi QTreeView by default uses QStyledItemDelegate to paint its items and that uses the locale object to get the display text from the double value returned from the model data. See source. By default the precision is set to 6 digits, so either your particular locale overrides that or you have a custom item delegate that does the double to string conversion differently.

              In any case you can provide a custom delegate if you want to override the way it displays numbers.

              Christian EhrlicherC 1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                @WuYaZi QTreeView by default uses QStyledItemDelegate to paint its items and that uses the locale object to get the display text from the double value returned from the model data. See source. By default the precision is set to 6 digits, so either your particular locale overrides that or you have a custom item delegate that does the double to string conversion differently.

                In any case you can provide a custom delegate if you want to override the way it displays numbers.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Chris-Kawa Or simply return the correct formated value in data() for Qt::DisplayRole (this is what this role is for).

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Chris KawaC 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Chris-Kawa Or simply return the correct formated value in data() for Qt::DisplayRole (this is what this role is for).

                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #7

                  @Christian-Ehrlicher Well yes, but also no :) data() returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpret DisplayRole is "the data to be displayed", not "how to display the data".

                  W 1 Reply Last reply
                  2
                  • Chris KawaC Chris Kawa

                    @Christian-Ehrlicher Well yes, but also no :) data() returns a QVariant, which does not need to be a string. In case of numbers it's often preferable to return the actual type, like double or int and let the view display it appropriately. This allows to have different views display the data from the same model differently e.g. have one view display a rounded number and another (e.g. a "detailed view") to show the full thing. You could do the conversion in the model, but it limits your further options and formatting shouldn't really be the model's job I think. The way I interpret DisplayRole is "the data to be displayed", not "how to display the data".

                    W Offline
                    W Offline
                    WuYaZi
                    wrote on last edited by Chris Kawa
                    #8

                    @Chris-Kawa thank for ur explaination. after custom my delegate , i solved this problem.alt text

                    and when click double value item, should reimplement setEditorData function to specify the format,otherwise the double value will shrink too. below is my key code,hope it helps someone else.

                    QWidget* XtrTaskViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
                    {
                    	return QStyledItemDelegate::createEditor(parent, option, index);
                    }
                    
                    void XtrTaskViewDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
                    {
                    	if (index.data().type() == QVariant::Double)
                    	{
                    		QDoubleSpinBox* doubleSpinBox = static_cast<QDoubleSpinBox*>(editor);
                    
                    		doubleSpinBox->setDecimals(10);
                    		doubleSpinBox->setValue(index.data().toDouble());
                    	}
                    	else 
                    	{
                    		QStyledItemDelegate::setEditorData(editor, index);
                    	}
                    }
                    
                    1 Reply Last reply
                    0
                    • W WuYaZi has marked this topic as solved on

                    • Login

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