我有QdoublespinxBox的问题
我正在使用Qdoublespinxbox作为col2中的委托,
但是当我有大麻时如99999999999999999999999999999999
他无法正确显示,他给我看了1e + 19
我用过这个
#include "customtableselldelegate.h"
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QDebug>
#include <databasemananger.h>
#include <QLocale>
customTableSellDelegate::customTableSellDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::createEditor(parent,option,index);
int col= index.column();
if(col == 0)
{
DataBaseMananger dbMgr;
QSqlQueryModel *queryModel = new QSqlQueryModel(parent);
queryModel->setQuery("SELECT articleDesignation FROM articles");
QComboBox *comboboxEditor = new QComboBox(parent);
comboboxEditor->setModel(queryModel);
//comboboxEditor->setEditable(true);
return comboboxEditor;
}
else if(col ==1 || col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
//doubleSpinBoxEditor->setSuffix(" D.A");
doubleSpinBoxEditor->setDecimals(2);
doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
doubleSpinBoxEditor->setFrame(false);
return doubleSpinBoxEditor;
}else{
return QStyledItemDelegate::createEditor(parent,option,index);
}
}
void customTableSellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::setEditorData(editor,index);
int col= index.column();
if(col == 0)
{
QString data = index.model()->data(index,Qt::DisplayRole).toString();
QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
comboboxEditor->setItemText(comboboxEditor->currentIndex(),data);
}
else if(col ==1 || col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
double data = index.model()->data(index,Qt::DisplayRole).toDouble();
QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
doubleSpinBoxEditor->setValue(data);
}else{
QStyledItemDelegate::setEditorData(editor,index);
}
}
void customTableSellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::setModelData(editor,model,index);
int col= index.column();
if(col == 0)
{
QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
model->setData(index,comboboxEditor->currentText(),Qt::EditRole);
emit unlockRow(index);
}
else if(col ==1 ||col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole);
if(col == 1 || col == 2)
{
emit qtyPriceDataChanged(index);
}
}else{
QStyledItemDelegate::setModelData(editor,model,index);}
}
void customTableSellDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void customTableSellDelegate::emitUnlockRow(QString str, QModelIndex index)
{
emit unlockRow(index);
}
#include "customtableselldelegate.h"
#include <QDoubleSpinBox>
#include <QComboBox>
#include <QDebug>
#include <databasemananger.h>
#include <QLocale>
customTableSellDelegate::customTableSellDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
QWidget *customTableSellDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::createEditor(parent,option,index);
int col= index.column();
if(col == 0)
{
DataBaseMananger dbMgr;
QSqlQueryModel *queryModel = new QSqlQueryModel(parent);
queryModel->setQuery("SELECT articleDesignation FROM articles");
QComboBox *comboboxEditor = new QComboBox(parent);
comboboxEditor->setModel(queryModel);
//comboboxEditor->setEditable(true);
return comboboxEditor;
}
else if(col ==1 || col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
QDoubleSpinBox *doubleSpinBoxEditor = new QDoubleSpinBox(parent);
doubleSpinBoxEditor->setRange(-999999999999999.99,999999999999999999.99);
//doubleSpinBoxEditor->setSuffix(" D.A");
doubleSpinBoxEditor->setDecimals(2);
doubleSpinBoxEditor->setButtonSymbols(QDoubleSpinBox::PlusMinus);
doubleSpinBoxEditor->setFrame(false);
return doubleSpinBoxEditor;
}else{
return QStyledItemDelegate::createEditor(parent,option,index);
}
}
void customTableSellDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::setEditorData(editor,index);
int col= index.column();
if(col == 0)
{
QString data = index.model()->data(index,Qt::DisplayRole).toString();
QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
comboboxEditor->setItemText(comboboxEditor->currentIndex(),data);
}
else if(col ==1 || col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
double data = index.model()->data(index,Qt::DisplayRole).toDouble();
QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
doubleSpinBoxEditor->setValue(data);
}else{
QStyledItemDelegate::setEditorData(editor,index);
}
}
void customTableSellDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(!index.isValid())
return QStyledItemDelegate::setModelData(editor,model,index);
int col= index.column();
if(col == 0)
{
QComboBox *comboboxEditor = qobject_cast<QComboBox*>(editor);
model->setData(index,comboboxEditor->currentText(),Qt::EditRole);
emit unlockRow(index);
}
else if(col ==1 ||col ==2 || col ==3 || col == 5 || col == 6 || col == 7)
{
QDoubleSpinBox *doubleSpinBoxEditor = qobject_cast<QDoubleSpinBox*>(editor);
model->setData(index,doubleSpinBoxEditor->value(),Qt::EditRole);
if(col == 1 || col == 2)
{
emit qtyPriceDataChanged(index);
}
}else{
QStyledItemDelegate::setModelData(editor,model,index);}
}
void customTableSellDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
void customTableSellDelegate::emitUnlockRow(QString str, QModelIndex index)
{
emit unlockRow(index);
}
但实际上并没有什么不同之处
答案 0 :(得分:1)
QDoubleSpinBox::textFromValue(double)
的默认实现返回包含打印值using
locale().toString(value, 'f', decimals());
除了浮点表示之外,永远不会返回任何内容。单独测试它肯定有效并产生正确的输出:
#include <QTextStream>
#include <QLocale
int main(int, char **)
{
QTextStream out(stdout);
QLocale sysLoc = QLocale::system();
double value = 99999999999999999999999999999999.;
double max = 999999999999999999.99;
int decimals = 2;
//Q_ASSERT(value < max);
QString str = sysLoc.toString(value, 'f', decimals);
str.remove(sysLoc.groupSeparator());
out << str << endl;
return 0;
}
100000000000000005366162204393472.00
顺便说一句,上面的输出是正确的,它只是说明了尾数的有限长度。
唉,你的最大值不足以显示1e19
或99999999999999999999999999999999.
,所以这可能是你的问题(注释掉的断言会触发),但它仍然不应该切换到科学的格式。
很可能您没有正确地将设置应用于您的spinbox,或者您的系统区域设置已搞砸。确保上面的测试代码正常工作。