如何从 Qt 中的 QListView 选定项获取 QString? [英] How to get QString from QListView selected item in Qt?

查看:39
本文介绍了如何从 Qt 中的 QListView 选定项获取 QString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在QListView 中获取所选项目名称作为QString.我试过谷歌,但我没有找到任何有用的东西.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆