![](/img/trans.png)
[英]Qt C++ | how can I get the string data that is linked to the index when I click an item in a QListWidget?
[英]How can I get the indices of QListWidget::selectedItems()?
我有QListWidget
,我需要獲取所選項目的索引。 (我可以按值使用列表中的項目,但是我有std::vector
,其中包含列表中每個項目的一些對象,我還需要從中刪除它們。)有一個功能indexFromItem
但是它是protected(?)。 ),所以我不能使用它。
有任何想法嗎?
QList<QListWidgetItem *> itemList = lw1->selectedItems();
您可以使用:
list->selectionModel()->selectedIndexes()
其中list
是QListWidget
的實例。
不知道它是否返回您想要的內容。
從文檔中 :
返回所有選定模型項索引的列表。 該列表不包含重復項,也不進行排序。
QListWidget
也有一個名為selectedIndexes
的方法,但是它是一個受保護的方法,因此您必須使用這種棘手的方法來獲取它們。
我不確定這是正確的方法,但是它對我有用:
void ScannerWindow::on_ipList_itemDoubleClicked(QListWidgetItem *item)
{
qDebug() << ui->ipList->row(item);
}
其中ui->ipList
是QListWidget
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.