问题描述
有没有办法将QModelIndex转换为QString?这个的主要目标是我想使用动态生成的QListView项目的内容。
QFileSystemModel * foolist = new QFileSystemModel;
foolist-> setRootPath(QDir :: rootPath());
foolistView-> setModel(foolist);
[...]
QMessageBox bar;
QString foolist_selectedtext = foolistView-> selectionModel() - > selectedIndexes();
bar.setText(foolist_selectedtext);
bar.exec;这是甚至是正确的方式来获得当前选择的项目吗?
提前感谢!
解决方案 foolistView-> selectionModel () - > selectedIndexes();
发送QModelIndex的QList(只有一个,如果你查看是在QAbstractItemView :: SingleSelection) / p>
QModelIndex方法返回一个对应于此索引值的QVariant。
您可以通过调用。
Is there a way to convert QModelIndex to QString? The main goal behind this is that I want to work with the contents of dynamically generated QListView-Items.
QFileSystemModel *foolist = new QFileSystemModel;
foolist->setRootPath(QDir::rootPath());
foolistView->setModel(foolist);
[...]
QMessageBox bar;
QString foolist_selectedtext = foolistView->selectionModel()->selectedIndexes();
bar.setText(foolist_selectedtext);
bar.exec;
Is this even the correct way to get the currently selected Item?
Thanks in advance!
解决方案 foolistView->selectionModel()->selectedIndexes();
Send you back a QList of QModelIndex (only one if you view is in QAbstractItemView::SingleSelection)
The data method of QModelIndex return a QVariant corresponding to the value of this index.
You can get the string value of this QVariant by calling toString on it.
这篇关于将QModelIndex转换为QString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!