简体   繁体   English

自定义QAbstractItemModel和QTableView中的选择损坏

[英]Broken selection in a custom QAbstractItemModel and QTableView

I'm working on a QTableView control with lazy loading. 我正在使用延迟加载的QTableView控件。

I have thousands of records it has to display and it used to lag badly when I used a simple QListWidget approach. 我有成千上万条必须显示的记录,而当我使用简单的QListWidget方法时,它的使用会非常滞后。

Now I use QAbstractItemModel with the following data method: 现在,我将QAbstractItemModel与以下data方法一起使用:

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

    if (role == Qt::DecorationRole && col == 0)
    {            
        return getIcon(row); // icons in the first column
    }
    else if (role == Qt::DisplayRole && col == 1)
    {
        return getText(row); // text in the second column            
    }
    else
    {
        return QVariant();
    }
}

The resulting table view works great: it is fast and smooth. 产生的表格视图效果很好:快速,流畅。

There's one major problem though: the selection is completely broken. 但是,有一个主要问题:选择被完全破坏了。

When I select an item/items, they are not highlighted in blue right away, I need to scroll the table so that it repaints and shows blue background. 当我选择一个项目/项目时,它们不会立即以蓝色突出显示,我需要滚动表格以便重新绘制并显示蓝色背景。 (I'm using Windows 7.) (我使用的是Windows7。)

Also I don't see the dotted rectangle when selecting items. 另外,选择项目时我看不到虚线矩形。

I checked, the selection model of the table view is not null. 我检查了,表视图的选择模型不为null。 Also I looked at some other model implementations in Qt, they have similar data method, but there's no selection problems with them. 我还查看了Qt中的其他一些模型实现,它们具有相似的数据方法,但是它们没有选择问题。

I also tried subclassing from QAbstractTableItem and QAbstractListItem , nothing. 我还尝试了从QAbstractTableItemQAbstractListItem子类,什么也没有。

Appreciate your help here. 在这里感谢您的帮助。

Sorry about this silly question... 对不起这个愚蠢的问题...

I solved this by removing the following line: 我通过删除以下行解决了此问题:

tableView->setRootIndex(model->index(0, 0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM