专门用于添加列的QAbstractProxyModel:表格单元格变空

时间:2016-12-06 03:16:35

标签: qt qtableview proxy-classes qabstractitemmodel

我创建了一个类似mixin的代理模型(Qt5),它只是向另一个代理模型添加了额外的第一列,用于向表视图的每一行添加QToolBar个动作(例如,& #34;删除&#34;按钮)。该模型仅提供了为第一列填充QList<QVariant>的方法。代表必须知道每个QVariant(通常是int s / enum标识操作)的含义,并相应地填充QToolBar。作为最后一项功能,如果没有操作,则不会添加额外的列(在这种情况下,它的行为类似于QIdentityProxyModel)。添加后,无法删除操作。这是另一天的特色。

今天的问题是,当我插入动作(我在将模型设置到视图之前执行)时,单元格都是空白。所以,我对信号做错了或谁知道什么(我认为错误在add_action函数中,在片段的末尾):

template<class proxy_model>
class action_model : public proxy_model
{
    QList<QVariant> l_actions;

public:
    using base_t = proxy_model;
    using base_t::base_t; // Inheriting constructors.

    QModelIndex mapFromSource(const QModelIndex& source_idx) const override
    {
         if (!l_actions.empty() and source_idx.isValid())
            return this->createIndex(source_idx.row(),
                                     source_idx.column() + 1);
         else // identity proxy case
            return base_t::mapFromSource(source_idx);
    } // same for mapToSource but with - 1 instead of + 1.

    int columnCount(const QModelIndex& parent = QModelIndex()) const override
    { return this->base_t::columnCount() + !l_actions.empty(); }

    QVariant headerData(int section, Qt::Orientation orientation, int role) const override
    {
         if (!l_actions.empty()) {

            if (orientation == Qt::Horizontal and section == 0
                and role == Qt::DisplayRole)
                return "Actions"; // Testing.
            else
                return base_t::headerData(section - 1, orientation, role);

         } else // identity proxy case
            return base_t::headerData(section, orientation, role);
    }

    QVariant data(const QModelIndex& idx, int role) const override
    {
        if (!l_actions.empty()) {
            if (idx.column() == 0 and role = Qt::DisplayRole)
                return l_actions; // All the actions for drawing.
            else
                return QVariant();
        } else // identity proxy case
             return base_t::data(idx, role);
    }

    Qt::ItemFlags flags(QModelIndex const& idx) const
    {
        if (!l_actions.empty() and idx.column() == 0)
            return Qt::NoItemFlags; // No editable or selectable
        else
            return base_t::flags(idx);
    }

    // And here, I think, is where the fun starts:
    // The action could be added before or after the sourceModel
    // is set or this model is connected to a view, but I don't
    // how that cases are supposed to be managed.
    void add_action(QVariant const& action)
    {
         bool was_empty = l_actions.empty();
         l_actions << action;

         if (was_empty and !this->insertColumns(0, 1))
            throw std::logic_error("Something went wrong");

         Q_EMIT this->dataChanged
             (this->createIndex(0, 0),
              this->createIndex(this->rowCount(), 0),
              { Qt::DisplayRole });
    }

};

如果不设置操作,则模型正常,QAbstractIdentityProxyModelQSortFilterProxyModelproxy_model。但是,在设置操作时,视图会将每个单元格显示为空白,包括QSortFilterProxyModelQAbstractIdentityProxyModel

这是一个用户土地代码:

enum sql_action { DELETE };

auto* table_model = /* My QSqlTableModel */;
auto* view_model = new action_model<QIdentityProxyModel>(my_parent);
auto* table_view = new QTableView;

view_model->add_action(static_cast<int>(sql_action::DELETE));
view_model->setSourceModel(table_model);

table_view->setModel(view_model);
table_view->setSortingEnabled(true);
table_view->setAlternatingRowColors(true);
// The last column is printed in white, not with alternate colors.

table_view->show();
table_model->select();

代表们不是问题,因为我没有设置任何人。我期待第一列有白色细胞,但我得到一个完全白色的桌子。列名显示正常,但最后一个列只显示0作为列名。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题在于您的data()方法。

  1. 您没有将role与分配给角色的Qt::DisplayRole进行比较
  2. 如果您有操作,则返回操作条目或QVariant(),绝不返回任何数据