使用 qt 浏览、列出和删除文件
我用 qt 设计器创建了以下表单。 我添加了一个“添加文件”按钮,该按钮与 QDir 和 QFileDialog 一起使用,并将文件加载到 listWidget 中。
以下是我用文件填充此表单的方法。
void RightDoneIt::changeDirectory()
{
/* select a directory using file dialog */
QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), directory.path());
if ( path.isNull() == false )
{
directory.setPath(path);
fillList();
}
}
/*get list of file from given directory and the append it to listWidget */
void RightDoneIt::fillList()
{
ui->listWidget->clear();
ui->listWidget->addItems(directory.entryList());
}
我想修改我的代码,以便可以在文件名旁边列出文件位置和文件大小,并使此删除文件按钮正常工作。
我只是希望能够使用 ctrl 或命令键(对于 Mac)选择文件,然后按删除键从我的列表中删除这些文件。
我必须使用 QtreeWidget 而不是 listwidget 吗?
这样做的最佳实践是什么?
有什么代码建议吗?
谢谢大家!
I have created the following form with qt designer.
I added a Add Files button that works with QDir and QFileDialog and loads files into a listWidget.
Here are my methods that fill this form with the files.
void RightDoneIt::changeDirectory()
{
/* select a directory using file dialog */
QString path = QFileDialog::getExistingDirectory (this, tr("Directory"), directory.path());
if ( path.isNull() == false )
{
directory.setPath(path);
fillList();
}
}
/*get list of file from given directory and the append it to listWidget */
void RightDoneIt::fillList()
{
ui->listWidget->clear();
ui->listWidget->addItems(directory.entryList());
}
I would like to modified my code so I can list the file location and the file size next to the file name and also to make this remove files button working.
I just want to be able to choose files using ctrl or command key(for macs) and press delete to remove these files from my list.
Do i have to use a QtreeWidget instead of listwidget ?
What are the best practices for doing that ?
any code suggestions ?
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只是列出文件(没有文件夹和子文件夹结构),则不需要
QTreeWidget
。但由于您愿意显示文件位置和文件大小,我将使用 QTableWidget(或 QTableView)。
不过,我建议看看
QFileSystemModel
。根据您尝试对应用程序执行的操作,此类可能会派上用场:您可以使用此模型并将其显示在视图小部件中。并且
QFileSystemModel
附带了诸如 remove() 并且还将处理文件重命名。If you are just listing files (no folder and subfolder structure), you don't need a
QTreeWidget
.But as you are willing to show the file location and file size, I would use a
QTableWidget
(orQTableView
).However, I would suggest to have a look at
QFileSystemModel
. Depending on what you're trying to do you with your app, this class might come handy : You can use this model and display it in a view widget.And
QFileSystemModel
comes with methods such as remove() and will also handle file renaming.