将 Qlistview 限制为最多 1 个选定项目
Limit Qlistview to 1 selected item at most
如果用户的选择更多为 1 并且包含第 1 项,我尝试强制程序在QListview
中选择第 1 项(并且仅选择它)。选择模式标志是多选,这意味着用户可以在listview
中选择多个项目。
下面是我的代码:
void RealPlay::when_allchannel_selected
(const QItemSelection &selected,
const QItemSelection &deselected)
{
// if the selected is the same with deseleted then
// just return, this is not necessary
QModelIndexList selectedlist = selected.indexes();
for(int i =0 ; i < selectedlist.length();++i)
{
if(!deselected.contains(selectedlist.at(i)))
{
break;
}
return;
}
// channelmodel QStandardItemModel
// selectedchannels QItemSelectionModel
// ui->listView_channel QListView
//this is the first item that I want to select
QModelIndex firstiteminchannelview =
channelmodel->indexFromItem(channelmodel->item(0));
if(selectedchannels->isSelected(firstiteminchannelview)
&& (selectedchannels->selectedIndexes().length()>1))
{
selectedchannels->reset();
selectedchannels->select(firstiteminchannelview,
QItemSelectionModel::Select);
//return;
}
//..
}
在构造函数中:
connect
(selectedchannels,
SIGNAL(selectionChanged(const QItemSelection &,const QItemSelection &)),
this,
SLOT(when_allchannel_selected(const QItemSelection &,const QItemSelection &)));
但是此代码不起作用。它仅取消选择用户在选择第 1 项之前所做的最后选择,而不是取消选择除第 1 项之外的所有其他项。我该怎么做?
其他问题:
QItemSelection
和QItemSelectionModel
的索引是否与QStandardItemModel
中的索引相同?
就像如果QStandardItemModel
中第1项的索引是0
,那么如果它被选中了,无论顺序是什么,索引仍然会在QItemSelection
和QItemSelectionModel
中0
。(似乎在线材料暗示它们是相同的..)
- 似乎
reset()
方法有效。但是为什么我仍然可以在ListView
中看到多个选择?
事实证明,我应该使用 QItemSelectionModel 的 select() 方法而不是 reset() 方法。下面是有效的代码。
if(selectedchannels->isSelected(firstiteminchannelview) && (selectedchannels->selectedIndexes().length()>1))
{
//selectedchannels->reset();
QModelIndex top = channelmodel->index(1,0);
QModelIndex bottom = channelmodel->index(channelmodel->rowCount()-1,0);
QItemSelection selection(top, bottom);
selectedchannels->select(selection,QItemSelectionModel::Deselect);
selectedchannels->select(firstiteminchannelview,QItemSelectionModel::Select);
//return;
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 更新 QTreeView 和 QListView 中的项目
- 无法检查 QListView 的项目
- 如何使用QListView项目添加复选框
- 从QListView项目中隐藏复选框
- Qt-检测QListView中的项目信息更改
- 将 Qlistview 限制为最多 1 个选定项目
- QListView:当从顶部删除项目时,如何自动滚动视图并保持视图中正确项目的当前选择
- 如何从QListView中获取所选项目
- 如何在选择多个项目时清除QListView的选择
- Qt5 C++:从QListView的模型中删除项目以及如何计算项目
- 从QStringListModel作为模型的QListview中删除单个项目
- 滚动QListView以在列表开始插入时保持项目在视图中
- QListView具有复选框选择行为的项目
- QListView项目背景颜色