#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
public:
MyDelegate
(QObject *parent
= 0) : QStyledItemDelegate
(parent
) { } if (value.
type() == QVariant::Double) return locale.
toString(value.
toDouble(),
'g',
16);
return QStyledItemDelegate::displayText(value, locale);
}
};
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
item->setData(3.8133400354, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text a"));
item->setData(2.123003885, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text h"));
item->setData(2.123003876, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text c"));
item->setData(123.85934000053, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text a"));
item->setData(13.85942403234, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text f"));
item->setData(46.8593400354, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text g"));
item->setData(3.2452334, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text d"));
item->setData(8.4223400354, Qt::DisplayRole);
m_model
->appendRow
(QList<QStandardItem
*>
() << item <<
new QStandardItem("text e"));
ui->tableView->setModel(m_model);
ui->tableView->setItemDelegate(new MyDelegate(this));
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
public:
MyDelegate(QObject *parent = 0) : QStyledItemDelegate(parent) { }
QString displayText(const QVariant &value, const QLocale &locale) const {
if (value.type() == QVariant::Double) return locale.toString(value.toDouble(), 'g', 16);
return QStyledItemDelegate::displayText(value, locale);
}
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_model = new QStandardItemModel(this);
QStandardItem *item = new QStandardItem;
item->setData(3.8133400354, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text a"));
item = new QStandardItem;
item->setData(2.123003885, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text h"));
item = new QStandardItem;
item->setData(2.123003876, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text c"));
item = new QStandardItem;
item->setData(123.85934000053, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text a"));
item = new QStandardItem;
item->setData(13.85942403234, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text f"));
item = new QStandardItem;
item->setData(46.8593400354, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text g"));
item = new QStandardItem;
item->setData(3.2452334, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text d"));
item = new QStandardItem;
item->setData(8.4223400354, Qt::DisplayRole);
m_model->appendRow(QList<QStandardItem *>() << item << new QStandardItem("text e"));
ui->tableView->setModel(m_model);
ui->tableView->setItemDelegate(new MyDelegate(this));
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
Bookmarks