我正在使用QListView,只有单个选择和选择更改由向上和向下箭头处理。一切都按预期工作,只有一个小问题。如果许多项目添加到列表顶部,则当前选定的项目可能会向下移动并退出视图。有没有办法检测何时发生这种情况?感谢。
答案 0 :(得分:2)
我找到了解决方案,但首先我尝试做类似的事情,我运行计时器,每隔2秒我将新行插入列表的开头并尝试检测,所选项仍然可见?
//somewhere in the constructor, it is not important
ListModel = new QStandardItemModel();
ui->listView->setModel(ListModel);
现在,当我想插入新行时,我编写了代码,检查是可见项
QStandardItem* Items = new QStandardItem(QString::number(qrand()%100));
ListModel->insertRow(0,Items);
// here we get rect of current selected item
QRect rec = ui->listView->visualRect(ui->listView->currentIndex());
//here we get all listView region, convert it to rect and check is our listView
//contain selected item
if(ui->listView->viewport()->visibleRegion().boundingRect().contains(rec))
{
qDebug() << "visible";//all good
}
else
{
qDebug() << "not visible";//not good, we need do something
//you want to detect it, so you get!
//now you know that item "runs away from you"
//and you can do some actions
someAction();
}
我在计算机上测试它,当我看到项目时,我会看到可见的单词,当项目尝试时#34;远离我&#34;我看不见了
我希望它有所帮助。