M
M
Mercury132015-12-11 19:23:24
Qt
Mercury13, 2015-12-11 19:23:24

QModelDelegate: why table and delegate have different precision and locale?

Here's the code.

void CoeffDelegate::setEditorData(
        QWidget* editor,
        const QModelIndex & index) const
{
    QLineEdit* currEdit = dynamic_cast<QLineEdit*>(editor);
    QString text = index.model()->data(index).toString();
    currEdit->setText(text);
}

model()->datais known to produce a QVariant; I make it from float. There are two problems.
1. There are about six significant figures in the table, eight or nine in the editor.
2. In the table, the system locale, in the editor, the standard one (with a dot).
What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2015-12-11
@Mercury13

Overriding QStyledItemDelegate. It has a function displayTextthat turns the value into text. You can redefine it (I don't really like what I originally liked, several million are already 5.67e + 6), but that's not the point.

QWidget* LineEditDelegate::createEditor(
        QWidget* parent,
        const QStyleOptionViewItem& option,
        const QModelIndex&) const
{
    QLineEdit* edit = new QLineEdit(parent);
    edit->setLocale(option.locale);   // важно! — локаль потом потребуется
    return edit;
}

void LineEditDelegate::setEditorData(
        QWidget* editor,
        const QModelIndex & index) const
{
    QLineEdit* currEdit = dynamic_cast(editor);
    QVariant var = index.model()->data(index);
    QString text = displayText(var, editor->locale());
    currEdit->setText(text);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question