如何从Qt中的QListView所选项目中获取QString? [英] How to get QString from QListView selected item in Qt?
本文介绍了如何从Qt中的QListView所选项目中获取QString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在QListView
中将选定的项目名称作为QString
.我曾尝试过Google,但没有发现任何有用的东西.
I need to get the selected item name in QListView
as a QString
. I have tried to google, but I haven't found anything useful.
推荐答案
这取决于selectionMode,可以说您具有ExtendedSelection
,这意味着您可以选择任意数量的项(包括0).
It depends on selectionMode lets say you have ExtendedSelection
which means you can select any number of items (including 0).
ui->listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
您应该遍历ui->listView->selectionModel()->selectedIndexes()
以找到所选项目的索引,然后调用text()
方法以获取项目文本:
you should iterate through ui->listView->selectionModel()->selectedIndexes()
to find indexes of selected items, and then call text()
method to get item texts:
QStringList list;
foreach(const QModelIndex &index,
ui->listView->selectionModel()->selectedIndexes())
list.append(model->itemFromIndex(index)->text());
qDebug() << list.join(",");
这篇关于如何从Qt中的QListView所选项目中获取QString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文