我有一个 QTableView 在 UI 文件中定义。这是图:

alt text

我想更改月份(其中 红色的 数组点)与 QComboBox 小部件,处理委托,但对我来说,对于我的自定义委托和模型来说,这是一个太复杂的问题,我无法弄清楚出了什么问题?!

问题:在我看来, QAbstractTableModel 不能与 QItemDelegate, ,因为我无法结合我的自定义简单 ComboBoxDelegate 带有 QTableView 的小部件。卧槽?

这是我所拥有的:

我的自定义委托标头/源数据:

class ComboBoxDelegate : public QItemDelegate
{
    Q_OBJECT

public:
    ComboBoxDelegate(QObject *parent = 0);

    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option,
        const QModelIndex &index) const;

    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                        const QModelIndex &index) const;

    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

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

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &index) const
{
    QComboBox* editor = new QComboBox(parent);
    editor->addItem(index.data(Qt::DisplayRole).toString());
    return editor;
}

void ComboBoxDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    comboBox->setCurrentIndex(comboBox->findText(value));
}

void ComboBoxDelegate::setModelData(QWidget *editor,
    QAbstractItemModel *model,
    const QModelIndex &index) const
{
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    QString value = comboBox->currentText();
    model->setData(index, value, Qt::EditRole);
}

型号数据:

class PortfolioModel : public QAbstractTableModel
{
    Q_OBJECT;

    // types
    enum Position
    {
          ePosValue = 0
        , eColumnCount
    };

    enum Constants
    {
          eLocalCcy = 0
            , eCurrentTime
        , eCurrentMonthName
        , eRowCount
    };

    // data
    static QFont    font_;
    static QBrush   foreground_;
    static QBrush   background_;

    // methods
    QVariant        _valueName(int index) const;
    QVariant        _valueNameTooltip(int index) const;
    QVariant        _data(int index, int col, bool tooltip) const;

public:
    PortfolioModel(QObject* parent = 0);
    ~PortfolioModel();

    int         rowCount(const QModelIndex& parent = QModelIndex()) const;
    int         columnCount(const QModelIndex& parent = QModelIndex()) const;

    QVariant        data(const QModelIndex& index, int role = Qt::DisplayRole) const;
    QVariant        headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

signals:
    void            resizeToContents(void);

public slots:
    void            refreshData(void);
};

QFont PortfolioModel::font_ = QFont("Tahoma", 8, QFont::Normal);
QBrush PortfolioModel::foreground_ = QBrush(QColor("#000000")); // Black
QBrush PortfolioModel::background_ = QBrush(QColor("#C3FDB8")); // Dark Sea Green1

PortfolioModel::PortfolioModel(QObject* parent)
    : QAbstractTableModel(parent)
{}

PortfolioModel::~PortfolioModel()
{}

void PortfolioModel::refreshData(void)
{
    emit dataChanged(QModelIndex(), QModelIndex());
    emit resizeToContents();
}

int PortfolioModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
{
    return eRowCount;
}

int PortfolioModel::columnCount(const QModelIndex& parent/* = QModelIndex()*/) const
{
    return eColumnCount;
}

QVariant PortfolioModel::data(const QModelIndex& index, int role/* = Qt::DisplayRole*/) const
{
    if (!index.isValid())
        return QVariant();

    switch (role)
    {
        case Qt::DisplayRole:
            return _data(index.row(), index.column(), false);
        case Qt::FontRole:
            break;
        case Qt::BackgroundRole:
            return background_;
        case Qt::ForegroundRole:
            return foreground_;
        case Qt::TextAlignmentRole:
        case Qt::DecorationRole:
        case Qt::EditRole:
            break;
        case Qt::ToolTipRole:
            return _data(index.row(), index.column(), true);
        case Qt::StatusTipRole:
        case Qt::WhatsThisRole:
        case Qt::SizeHintRole:
            break;
    }
    return QVariant();
}

QVariant PortfolioModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (orientation)
    {
    case Qt::Horizontal:
        switch (role)
        {
        case Qt::DisplayRole:
            if (section == ePosValue)
            {
                return QVariant("Value");
            }
        case Qt::ToolTipRole:
            if (section == ePosValue)
            {
                return QVariant("Fund values");
            }
            break;
        }
    case Qt::Vertical:
        switch (role)
        {
        case Qt::DisplayRole:
            return _valueName(section);
        case Qt::ToolTipRole:
            return _valueNameTooltip(section);
        }
        break;
    }

    return QVariant();
}

QVariant PortfolioModel::_valueNameTooltip(int index) const
{
    switch (index)
    {
    case eLocalCcy:
        return QObject::tr("Local currency");
    case eCurrentTime:
        return QObject::tr("Current time");
    case eCurrentMonthName:
        return QObject::tr("Current Month");
    }
    return QVariant();
}

QVariant PortfolioModel::_valueName(int index) const
{
    switch (index)
    {
    case eLocalCcy:
        return QObject::tr("Local Currency");
    case eCurrentTime:
        return QObject::tr("Current Time");
    case eCurrentMonthName:
        return QObject::tr("Month");
    }
    return QVariant();
}

QVariant PortfolioModel::_data(int index, int col, bool tooltip) const
{
    switch (index)
    {
    case eLocalCcy:
        if (col == ePosValue)
        {
            return QString(Nav::bk()->currentFund().currency(Nav::bk()->currentFund().localCcy()).code());
        }
        break;
    case eCurrentTime:
        if (col == ePosValue)
        {
            return Nav::bk()->currentFund().currentTime();
        }
        break;
    case eCurrentMonthName:
        if (col == ePosValue)
        {
            return QDate::longMonthName(Nav::bk()->currentFund().currentTime().date().month());
        }
        break;
    }
    return QVariant();
}

之后我像这样创建了 init 委托:

ComboBoxDelegate *delegate_ = new ComboBoxDelegate(this);
this->ui.tableView->setItemDelegate(delegate_);

附:抱歉输出太长,但我认为如果所有来源都在这里呈现会更好。

谢谢!


  1. 添加:
有帮助吗?

解决方案

从概述来看 QA抽象项目模型 (参见 子类化 标题):

要在模型中启用编辑,您还必须实现setData()和重新进化flags(),以确保返回该项目。

概述中给出了相同的信息 QA抽象表模型. 。你的 PortfolioModel 类不会重新实现这两个函数。为了使用委托的编辑器,您需要使用 do so。

或者,您可能只希望它看起来像有一个组合框(实际上不允许编辑)。如果是这种情况,您可能需要自己进行一些绘图技巧才能使其看起来像这样......或者将其标记为可编辑但禁用小部件,或类似的东西。

其他提示

在这个博客中 http://qt-articles.blogspot.com/2010/07/how-to-customize-listview-in-qt-using.html 给出了一个代表的例子..请使用博客中存在的委托示例检查您的委托。希望你能得到一些线索。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top