在QTableView中格式化数据

在QTableView中格式化数据,qt,qtableview,qitemdelegate,Qt,Qtableview,Qitemdelegate,我正在使用自定义委托在QTableView中显示QdoubleSpinbox。这些微调框以两位小数显示其内容 我的问题是,我希望QTableView在不编辑这些数字时(此时它们不在QDoubleSpinBox中)也显示带有两个小数的数字。或者,更确切地说,我希望能够为QTableView的内容指定一种格式 我试图将QStyledItemDelegate子类化以覆盖displayText,但由于一个奇怪的原因,它崩溃了。如果我只是将QItemDelegate子类化,它就可以正常工作 我正在Wind

我正在使用自定义委托在QTableView中显示QdoubleSpinbox。这些微调框以两位小数显示其内容

我的问题是,我希望QTableView在不编辑这些数字时(此时它们不在QDoubleSpinBox中)也显示带有两个小数的数字。或者,更确切地说,我希望能够为QTableView的内容指定一种格式

我试图将QStyledItemDelegate子类化以覆盖displayText,但由于一个奇怪的原因,它崩溃了。如果我只是将QItemDelegate子类化,它就可以正常工作


我正在Windows上使用Qt4.6.3。

我不知道发生了什么,但现在它不再崩溃。现在它可以工作了

对于记录,这是我的displayText方法:

QString sqxSpinBoxDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
    return locale.toString(value.toDouble(), 'f', Decimals);
}

我不知道发生了什么,但现在它不再崩溃了。现在它可以工作了

对于记录,这是我的displayText方法:

QString sqxSpinBoxDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
    return locale.toString(value.toDouble(), 'f', Decimals);
}

我真的不知道如何处理你得到的这个例外。下面是一个简单的QStyledItemDelegate,我们正在使用它,没有任何问题。也许有什么不同

#include "model_view/color_combo_delegate.h"

#include <QTimer>

#include "map_elements/common/color_combo_box.h"

ColorComboDelegate::ColorComboDelegate(QObject *parent)
  : QStyledItemDelegate(parent) {
}

QWidget *ColorComboDelegate::createEditor(
    QWidget *parent,
    const QStyleOptionViewItem & /*option*/,
    const QModelIndex & /*index*/) const {
  ColorComboBox *color_combo_box = new ColorComboBox(parent);
  connect(color_combo_box, SIGNAL(currentIndexChanged(int)),
          this, SLOT(IndexChanged()));
  QTimer::singleShot(0, color_combo_box, SLOT(Popup()));
  return color_combo_box;
}

QString ColorComboDelegate::displayText(const QVariant &value,
                                        const QLocale &/*locale*/) const {
  Map::Color color = static_cast<Map::Color>(value.toInt());
  return Map::color_name(color);
}

void ColorComboDelegate::IndexChanged() {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(sender());
  emit commitData(color_combo_box);
  emit closeEditor(color_combo_box);
}

void ColorComboDelegate::setEditorData(QWidget * editor,
                                       const QModelIndex & index) const {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(editor);
  Map::Color color = static_cast<Map::Color>(index.data().toInt());
  color_combo_box->set_color(color);
}

void ColorComboDelegate::setModelData(QWidget *editor,
                                      QAbstractItemModel *model,
                                      const QModelIndex &index) const {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(editor);
  model->setData(index, color_combo_box->color());
}
#包括“model_view/color_combo_delegate.h”
#包括
#包括“map\u elements/common/color\u combo\u box.h”
ColorComboDelegate::ColorComboDelegate(QObject*parent)
:QStyledItemDelegate(父级){
}
QWidget*ColorComboDelegate::createEditor(
QWidget*父项,
常量QStyleOptionViewItem&/*选项*/,,
常数QModelIndex&/*索引*/)常数{
ColorComboBox*color\u combo\u box=新的ColorComboBox(父项);
连接(颜色组合框,信号(currentIndexChanged(int)),
这个插槽(IndexChanged());
QTimer::单发(0,彩色组合框,插槽(Popup());
返回颜色组合框;
}
QString ColorComboDelegate::displayText(常量QVariant和值,
常量QLocale&/*区域设置*/)常量{
Map::Color=static_cast(value.toInt());
返回映射::颜色\名称(颜色);
}
void ColorComboDelegate::IndexChanged(){
ColorComboBox*color\u combo\u box=qobject\u cast(sender());
发出提交数据(颜色组合框);
发射关闭编辑器(颜色组合框);
}
void ColorComboDelegate::setEditorData(QWidget*编辑器,
常数QModelIndex和索引)常数{
COLORCOMBOX*color\U COMBOX=qobject\U cast(编辑器);
Map::Color Color=static_cast(index.data().toInt());
颜色组合框->设置颜色(颜色);
}
void ColorComboDelegate::setModelData(QWidget*编辑器,
QAbstrateModel*模型,
常数QModelIndex和索引)常数{
COLORCOMBOX*color\U COMBOX=qobject\U cast(编辑器);
模型->设置数据(索引、颜色组合框->颜色();
}

我真的不确定如何处理您遇到的异常。下面是一个简单的QStyledItemDelegate,我们正在使用它,没有任何问题。也许有什么不同

#include "model_view/color_combo_delegate.h"

#include <QTimer>

#include "map_elements/common/color_combo_box.h"

ColorComboDelegate::ColorComboDelegate(QObject *parent)
  : QStyledItemDelegate(parent) {
}

QWidget *ColorComboDelegate::createEditor(
    QWidget *parent,
    const QStyleOptionViewItem & /*option*/,
    const QModelIndex & /*index*/) const {
  ColorComboBox *color_combo_box = new ColorComboBox(parent);
  connect(color_combo_box, SIGNAL(currentIndexChanged(int)),
          this, SLOT(IndexChanged()));
  QTimer::singleShot(0, color_combo_box, SLOT(Popup()));
  return color_combo_box;
}

QString ColorComboDelegate::displayText(const QVariant &value,
                                        const QLocale &/*locale*/) const {
  Map::Color color = static_cast<Map::Color>(value.toInt());
  return Map::color_name(color);
}

void ColorComboDelegate::IndexChanged() {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(sender());
  emit commitData(color_combo_box);
  emit closeEditor(color_combo_box);
}

void ColorComboDelegate::setEditorData(QWidget * editor,
                                       const QModelIndex & index) const {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(editor);
  Map::Color color = static_cast<Map::Color>(index.data().toInt());
  color_combo_box->set_color(color);
}

void ColorComboDelegate::setModelData(QWidget *editor,
                                      QAbstractItemModel *model,
                                      const QModelIndex &index) const {
  ColorComboBox *color_combo_box = qobject_cast<ColorComboBox *>(editor);
  model->setData(index, color_combo_box->color());
}
#包括“model_view/color_combo_delegate.h”
#包括
#包括“map\u elements/common/color\u combo\u box.h”
ColorComboDelegate::ColorComboDelegate(QObject*parent)
:QStyledItemDelegate(父级){
}
QWidget*ColorComboDelegate::createEditor(
QWidget*父项,
常量QStyleOptionViewItem&/*选项*/,,
常数QModelIndex&/*索引*/)常数{
ColorComboBox*color\u combo\u box=新的ColorComboBox(父项);
连接(颜色组合框,信号(currentIndexChanged(int)),
这个插槽(IndexChanged());
QTimer::单发(0,彩色组合框,插槽(Popup());
返回颜色组合框;
}
QString ColorComboDelegate::displayText(常量QVariant和值,
常量QLocale&/*区域设置*/)常量{
Map::Color=static_cast(value.toInt());
返回映射::颜色\名称(颜色);
}
void ColorComboDelegate::IndexChanged(){
ColorComboBox*color\u combo\u box=qobject\u cast(sender());
发出提交数据(颜色组合框);
发射关闭编辑器(颜色组合框);
}
void ColorComboDelegate::setEditorData(QWidget*编辑器,
常数QModelIndex和索引)常数{
COLORCOMBOX*color\U COMBOX=qobject\U cast(编辑器);
Map::Color Color=static_cast(index.data().toInt());
颜色组合框->设置颜色(颜色);
}
void ColorComboDelegate::setModelData(QWidget*编辑器,
QAbstrateModel*模型,
常数QModelIndex和索引)常数{
COLORCOMBOX*color\U COMBOX=qobject\U cast(编辑器);
模型->设置数据(索引、颜色组合框->颜色();
}

您所描述的内容应该有效。(我们做了类似的事情。)您有关于崩溃的更多信息吗?“0xC0000005:访问冲突写入位置0x00000018。”崩溃发生在QBasicAtomicInt::deref中,但奇怪的是,在堆栈跟踪中,下面的两个调用中,有一个对QItemDelegate::paint的调用。但我使用的是QStyledItemDelegate…你所描述的应该有用。(我们做了类似的事情。)您有关于崩溃的更多信息吗?“0xC0000005:访问冲突写入位置0x00000018。”崩溃发生在QBasicAtomicInt::deref中,但奇怪的是,在堆栈跟踪中,下面的两个调用中,有一个对QItemDelegate::paint的调用。但我用的是QStyledItemDelegate。。。