首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QDateEdit委托的问题

QDateEdit委托的问题
EN

Stack Overflow用户
提问于 2014-05-06 13:49:11
回答 1查看 1.9K关注 0票数 1

我需要能够在QTableView中编辑日期,所以我实现了QItemDelegate:

代码语言:javascript
复制
#include "dateeditdelegate.h"

DateEditDelegate::DateEditDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget *DateEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QDateEdit * editor = new QDateEdit(parent);
    return editor;
}

void DateEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
    dateEdit->setDate(index.model()->data(index).toDate());
}

void DateEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
    model->setData(index, dateEdit->dateTime().toTime_t());
}

void DateEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

我还重新实现了QSqlRelationalTableModel:

代码语言:javascript
复制
QVariant TblModel::data(const QModelIndex &item, int role) const
{
    if(item.column() == this->fieldIndex("add_date")){
        return QVariant(QDateTime::fromTime_t(QSqlQueryModel::data(item, role).toUInt()).date());
    }
    return QSqlQueryModel::data(item, role);
}

但结果我得到了

这些字段是可编辑的,但是编辑后不会保存新值(我可以将任何字段更改为"01.01.2015",但按Enter后,它将恢复为"03.04.14")。另外,我不明白为什么会出现这些复选框。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-07 14:06:51

您的主要问题是您不了解Qt提供的整个MVC框架。

您不需要自定义委托。默认情况下,QItemDelegate已经知道如何为日期/时间数据创建编辑器。

问题的第一部分的解决方案在于重新实现模型的data()

QSqlQueryModel被显式定义为只读模型.如果希望编辑数据,则需要创建自己的存储和编辑底层数据的机制,通过重新实现setData()函数将对其进行更改。

如果你想得到你想要的结果,你需要迎合不同的角色。为了您的目的,您需要为Qt::DisplayRoleQt::EditRoleQt::TextAlignmentRole实现

Qt::DisplayRole是视图上显示的内容,最常见的格式是QString

Qt::EditRole是给委托的数据,这样它就可以创建适当的编辑器小部件并给它正确的数据。

Qt::TextAlignmentRole用于告诉视图如何将调用返回的数据与Qt::DisplayRole对齐。

代码语言:javascript
复制
QVariant TblModel::data(const QModelIndex &item, int role) const{

    if(item.column() == this->fieldIndex("add_date")){
        if (role == Qt::TextAlignmentRole){
            return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
        }

        QDateTime dateTime = QDateTime::fromTime_t(QSqlQueryModel::data(item, role).toUInt());
        if (role == Qt::DisplayRole){
            return dateTime.toString("dd.MM.yyyy");
        }
        if (role == Qt::EditRole){
            return dateTime.date();
        }
    }
    return QSqlQueryModel::data(item, role);
}

如果希望您的数据是可编辑的,则需要使用与QSqlQueryModel不同的基本模型。您尚未实现的下一部分是更改模型中的数据。您可以通过重新实现setData()函数来做到这一点。

代码语言:javascript
复制
bool TblModel::setData(QModelIndex const &index, QVariant const &value, int role){
    if (!index.isValid() || role == Qt::EditRole){
        return false;
    }

    if (index.column() == fieldIndex("add_date")){
        // modify the underlying data

        if (validEdit){
            emit dataChanged(index, index); // signal to the view that the item needs to be redrawn
            return true;
        }
    }

    return false;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23496583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档