使用 qt 浏览、列出和删除文件

发布于 2024-10-03 06:16:43 字数 905 浏览 1 评论 0原文

我用 qt 设计器创建了以下表单。 我添加了一个“添加文件”按钮,该按钮与 QDir 和 QFileDialog 一起使用,并将文件加载到 listWidget 中。

alt text

以下是我用文件填充此表单的方法。

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.

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-10 06:16:43

如果您只是列出文件(没有文件夹和子文件夹结构),则不需要 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 (or QTableView).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文