首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >专门化用于添加列的QAbstractProxyModel :表单元格变为空

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

Stack Overflow用户
提问于 2016-12-06 03:16:35
回答 1查看 1K关注 0票数 0

我已经创建了一个类似于混合的代理模型(Qt5),它只是向另一个代理模型添加了一个额外的第一列,用于向表视图的每一行添加一个操作的QToolBar (例如,一个“删除”按钮)。该模型只是提供了一种为第一列填充QList<QVariant>的方法。委托必须知道每个QVariant的含义(通常是ints/enum的标识操作),并相应地填充QToolBar。作为最后一个特性,如果没有操作,则不会添加额外的列(在这种情况下,它的行为类似于QIdentityProxyModel )。一旦添加,操作就不能删除。这是另一天的特写。

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

代码语言:javascript
复制
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 });
    }

};

在不设置操作的情况下,模型可以正常工作,QAbstractIdentityProxyModelQSortFilterProxyModel都可以作为proxy_model。但是,在设置操作时,视图会显示每个单元格空白,包括QSortFilterProxyModelQAbstractIdentityProxyModel

这是一个用户土地代码:

代码语言:javascript
复制
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打印为列名。

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2016-12-07 15:39:57

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

  1. 您不能将role与指定给角色的Qt::DisplayRole进行比较。
  2. 如果有操作,则返回操作项或QVariant(),而不是任何数据。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40987240

复制
相关文章

相似问题

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