Qt QAbstractModel:删除复选框 [英] Qt QAbstractModel: remove checkbox

查看:261
本文介绍了Qt QAbstractModel:删除复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Qt,我想通过QTableView实现一个充满数据的表.我的问题是,我不知道如何从单元格中删除复选框.似乎它们是默认设置的.

I started to learn Qt, and I would like to implement a table filled with data via QTableView. My problem is, that I don't know how to remove the checkboxes from the cells. It seems like they are put in by default.

但是,我读到我必须返回一个NULL-QVariant,但这不是我想要的,因为我仍然要放入数据.

However, I read that I had to return a NULL-QVariant, but that's not what I was looking for as I still have data to put in.

到目前为止,这是我的代码:

That's my code so far:

QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
QString daten;

switch (col)
{
case 0:
{
    daten = "column 1";
    break;
}
case 1:
{
    daten = "column 2";
    break;
}
case 2:
{
    daten = "column 3";
    break;
}
case 3:
{
    daten = "column 4";
    break;
}
}

return daten;
}

现在,如您所见,我想用名为"daten"的QString填充单元格.但是在字符串旁边,每个单元格中都有一个复选框.

Now, as you can see, I want to fill the cell with the QString called "daten". But next to the String there is a Checkbox in every cell.

有人知道如何删除该复选框,但仍然用"daten"填充内容吗?

Does somebody know how to remove the checkbox but still fill the content with "daten"?

谢谢!

推荐答案

QTableView中的单元格具有某些复选框的事实表明它们已定义为用户可检查的.检查您是否没有在QTableView定义中的某个地方激活Qt.ItemIsUserCheckable标志,如果是这种情况,请将其停用.您可以尝试修改flags方法,例如,强制每个条目 not 都是可检查的

The fact that the cells in your QTableView have some checkbox hint that they were defined as user-checkable. Check whether you don't have a Qt.ItemIsUserCheckable flag activated somewhere in the definition of your QTableView, and if that's the case, deactivate it. You could try to modify the flags method, for example, forcing every entry not to be checkable

作为附加注释,您可能应该修改::data方法,以考虑index无效的情况,并仅在角色对应于Qt.DisplayRole时才返回某些值.在Python中,语法为

As an additional comment, you should probably modify your ::data method to take into account the case where index is invalid and to return some value only if the role corresponds to Qt.DisplayRole. In Python, the syntax would be

if index.isvalid():
    if (role == Qt.DisplayRole):
        (row, col) = (index.row(), index.column()
        return_something_depending_on_col
    return QVariant()
return QVariant()

这样,您可以覆盖无效索引的情况,否则代码可能会崩溃.

That way, you cover the case of an invalid index, your code would likely crash otherwise.

role上的测试允许您选择要访问的数据类型. 文档指出例如:

The test on role allows you to choose which type of data you want to access. The documentation states for example that:

模型中的每个项目都有一组与之关联的数据元素,每个元素都有其自己的角色.视图使用角色来向模型指示其需要哪种数据类型.自定义模型应返回这些类型的数据.

Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.

基本角色是Qt.DisplayRole,您将在其中返回与当前单元格相对应的QString.如果您的角色是Qt.BackgroundRole ...

The basic role is Qt.DisplayRole, where you return the QString corresponding to your current cell. You could also return a QBrush for painting the background if your role is Qt.BackgroundRole...

尽管不是强制性的,但仍然强烈建议您在role上进行这些测试:它使您的代码更清洁,更易于维护.

While not mandatory, these tests on role are still highly encouraged: it makes your code cleaner and easier to maintain.

这篇关于Qt QAbstractModel:删除复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆