复选框在QTableView的标题单元格 [英] Checkbox in a header cell in QTableView

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

问题描述

我想有一个简单的列标题,其中有一个复选框可以选择/取消选择QTableView中的所有行。单击标题中的复选框可以选择或取消选择所有行。

I want to have a simple column header with a checkbox that selects/ deselects all rows in a QTableView. Clicking the check box in the header causes either to select or deselect all rows.

>

当我想在表格单元格中添加一个复选框时,我必须在所需的模型索引的数据(..)中返回Qt :: CheckStateRole的检查状态下面。

When I want to add a check box in a table cell, I have to just return the check state for the Qt::CheckStateRole in the data(..) for the required model indices as below. This is working as expected.

QVariant MyModel::data( const QModelIndex & rIndex, int iRole) const
{
    ...

    if (iRole == Qt::Qt::CheckStateRole)
    {
        return checkstate;
    }

}

但是当我想添加复选框在标题单元格中,上述方法不工作。 Hear是我的示例代码。

But when I want to add a checkbox in a header cell, the above method is not working. Hear is my sample code.

QVariant MyModel::headerData( int iSection, Qt::Orientation eOrientation, int iRole) const
{
    ...

    if (iRole == Qt::CheckStateRole)
    {
        return checkstate;
    }

}

QTableView不调用headerData )函数在我的模型与Qt :: CheckStateRole,因为它与data()函数。

The QTableView does not call headerData() function in my model with the Qt::CheckStateRole, as it does with data() function.

为什么会出现这种情况?如何通过只修改我的自定义表模型在标题单元格中插入复选框?

Why is this behavior? How can I insert a check box in a header cell by only modifying my custom table model?

(我不想为此目的创建自定义QTableView或QHeaderView)

(I do not want to create a custom QTableView or QHeaderView for this purpose)

推荐答案

你不能这样做 - 默认情况下Qt不支持标题中的复选框。您可以阅读 https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F for更多信息及其实现使用自定义 QHeaderView

You cannot do it - Qt doesn't support check boxes in headers by default. You can read https://wiki.qt.io/Qt_project_org_faq#How_can_I_insert_a_checkbox_into_the_header_of_my_view.3F for further information and its realization using custom QHeaderView

这篇关于复选框在QTableView的标题单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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