我想在QListWidget中,添加选中的文件名,在QListWidget的每一个item保存的是完整的路径名,比如/home/a/a.mp4,但是我在QListWidget的显示出来是a.mp4。
我的做法是:
listWidget->insertItem(xxx);
这样显示出来是完整的路径名的,与我的目标是不一样的。请问该怎么实现,谢谢!
------解决方案--------------------
xxx 是 QListWidgetItem 吧
QListWidgetItem 有一个方法叫做
void QListWidgetItem::setText ( const QString & text )
------解决方案--------------------
QString QFileInfo::fileName () const
Returns the name of the file, excluding the path.
Example:
QFileInfo fi("/tmp/archive.tar.gz");
QString name = fi.fileName(); // name = "archive.tar.gz"
Note that, if this QFileInfo object is given a path ending in a slash, the name of the file is considered empty.
------解决方案--------------------
void QListWidgetItem::setText ( const QString & text )是设置QListWidget的显示文本.
如果想保存其他的信息,可以这样:
pListWidgetItem->setData( Qt::UserRole, QVariant(userData) );
userData就是你想保存的信息,这里可以是完整路径。
取出来:pListWidgetItem->data( Qt::UserRole );
------解决方案--------------------
只需要在创建一个 QStringList 保存所有的完整文件路径
然后再添加一个updataList方法
根绝QStringList的内容更新 QWidgetList就可以了