如何从QListView中获取所选项目
How do I get the items selected from a QListView?
{
...
nrow = 10;
ncol = 1;
/*create QListView */
m_listView = new QListView(this);
m_listView->setGeometry(QRect(QPoint(0,100), QSize(100, 150)));
QStandardItemModel *model = new QStandardItemModel( nrow, 1, this );
//fill model value
for( int r=0; r<nrow; r++ )
{
QString sstr = "[ " + QString::number(r) + " ]";
QStandardItem *item = new QStandardItem(QString("Idx ") + sstr);
model->setItem(r, 0, item);
}
//set model
m_listView->setModel(model);
m_listView->setSelectionMode( QAbstractItemView::ExtendedSelection );
connect(m_listView, SIGNAL(pressed(QModelIndex)), this, SLOT(hItem(QModelIndex)));
}
void MainWindow::hItem(QModelIndex m)
{
QItemSelectionModel *selectionModel = m_listView->selectionModel();
m_txt2->setText(QString::number(selectionModel->selectedIndexes().at(0),'d',0));//???
//not sure how to get the items selected: index and string per selection
}
我只是根据自己的需要测试了它,它在Qt 5.1中有效。
我对C++还很陌生,所以在这一行:
foreach(const QModelIndex &index, list){
我不知道是否需要const
和取消引用(&
)——不管有没有,它都能工作。我从我看到的各种例子中拼凑而成。
也许更了解C++的人可以发表评论。
void MainWindow::on_keywordsList_clicked(const QModelIndex &index)
{
QModelIndexList list =keywordListView->selectionModel()->selectedIndexes();
QStringList slist;
foreach(const QModelIndex &index, list){
slist.append( index.data(Qt::DisplayRole ).toString());
}
qDebug() << slist.join(",");
}
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- C++项目编译强制使用 /clr 选项,尽管在没有公共语言运行时支持的情况下执行它
- 暂时将C++项目(VS2015)移至Android操作系统.编译器选项?
- 是否有任何 MSBuild 命令行选项可以动态禁用 c++ 项目的代码分析?
- 用于在 Visual Studio 中为整个项目或解决方案定义带有参数的预处理器宏的选项
- 如何将cacert.pem与vs2015 curl项目一起包含,因此无需指定Cainfo curl选项
- Visual Lint?中的每个项目选项
- C++:使用 g++ 和 -m32 选项构建的 c++ 库在 Msys 下构建 wxWidgets 项目时出现 ld 不
- 如何在 eclipse makefile 项目中的编译器选项中设置 -std=c++0x
- 无法使项目,无法识别的命令行选项libc ++错误
- 将cmake构建选项导出到外部项目
- 为什么c++透视图和新项目选项在Eclipse中不可用
- 4.9.9.2项目选项不工作
- 无法使用/ CLR选项从Visual c++项目加载CLR
- 如何将code::块设置为在目标链接器选项之前链接项目链接器选项
- 可以qmake-configure Visual Studio项目中的命令行选项
- 在我的项目中使用预编译的头(clang/llvm,但也使用gcc).如何在make中部署-include选项
- 从下拉框中选择其他项目时更改选项卡小部件的 currentIndex()
- 使用 /Command 选项从命令行为 Visual Studio C++ 项目创建一个新文件
- 向Eclipse CDT Autotools项目添加链接器选项