如何浏览 C++ 中的文件夹还是C?

发布于 2024-11-19 20:12:58 字数 169 浏览 4 评论 0原文

我正在用 C(也在 C++)中实现一个程序来使用线程对文件进行排序,我需要用 C++ 或 C 实现一个 GUI 来选择要排序的文件,而不通过输入标准指示路径(相当于 Java 中的 JFileChooser) 。您推荐哪些教程?我正在阅读有关 Qt 的内容,但我对这个 IDE 不是很熟悉。如果你有任何例子会对我有很大帮助。

I am implementing a program in C (also in C++) to sort files using threads, I need to implement a GUI in C++ or C to select the file to sort, without indicating the path through input standard (a equivalent of JFileChooser in Java). What tutorials do you recommend? I was reading about Qt but I'm not very familiar with this IDE. If you have any example would help me a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

我不吻晚风 2024-11-26 20:12:58

使用 QT 非常简单:

 void openFile()
  {
    QFileDialog::getOpenFileName( this, tr("Open Document"), QDir::currentPath(), tr("Document files (*.doc *.rtf);;All files (*.*)"), 0, QFileDialog::DontUseNativeDialog );

    QString filename = QFileDialog::getOpenFileName( 
        this, 
        tr("Open Document"), 
        QDir::currentPath(), 
        tr("Document files (*.doc *.rtf);;All files (*.*)") );
    if( !filename.isNull() )
    {
      qDebug( filename.toAscii() );
    }
  }

Very simple with QT:

 void openFile()
  {
    QFileDialog::getOpenFileName( this, tr("Open Document"), QDir::currentPath(), tr("Document files (*.doc *.rtf);;All files (*.*)"), 0, QFileDialog::DontUseNativeDialog );

    QString filename = QFileDialog::getOpenFileName( 
        this, 
        tr("Open Document"), 
        QDir::currentPath(), 
        tr("Document files (*.doc *.rtf);;All files (*.*)") );
    if( !filename.isNull() )
    {
      qDebug( filename.toAscii() );
    }
  }
岁月静好 2024-11-26 20:12:58

您不必为此使用带有主循环的完整 GUI。 windows 和unix 提供显示所需文件对话框的函数调用。在 Windows 上,您可以调用 GetOpenFileName 或 GetSaveFileName。如果您需要跨平台的方式来做到这一点,请尝试 sourceforge 上的小文件对话框。

you don't have to use a full gui with a main loop just for that. windows and unix offers function calls that display the required file dialogs. On windows you would call GetOpenFileName or GetSaveFileName. If you need a cross-platform way to do it, try tiny file dialogs on sourceforge.

多情癖 2024-11-26 20:12:58

是的,您可以使用QT。您可以使用 QDirQT 用于浏览文件夹和QFile文件处理类。

有关示例,请参阅QT 文档。它有很多。

Yes you can use QT. You can use the QDir class of QT for browsing folders and QFile class for file handling.

See the QT doccumentaion for examples. It has a lot of them.

糖粟与秋泊 2024-11-26 20:12:58

你有两个选择:

  1. 如果你想使用QT,你应该使用QFileDialog 并将 Options 标志设置为QFileDialog::ShowDirsOnly。

  2. 如果您只想为 Windows 编写代码,可以使用 FolderBrowserDialog 类是 Win32 的一部分。

You have two options:

  1. If you want to use QT, you should use QFileDialog and set Options flag to QFileDialog::ShowDirsOnly.

  2. If you want to code only for Windows you can use FolderBrowserDialog class which is part of Win32.

对风讲故事 2024-11-26 20:12:58

还有 QFileSystemModel,它可以映射到 QTreeView 项,并通过其内部实现完成您需要的许多操作。
它是多线程的,允许排序并具有许多其他不错的功能。在此处查找文档

There is also QFileSystemModel, which can be mapped to QTreeView item and will do many things you need with its internal implementation.
It is multithreaded, allows sorting and have many other nice features. look for documentation here

讽刺将军 2024-11-26 20:12:58

如果排序是你的程序所做的唯一事情,并且如果它要在 unix-y 环境中使用,那么我建议将该程序设为命令行工具,并用某种脚本语言创建一个 GUI 包装器来调用该程序。编码将更简单,也更有用(您只需花费半个工具的价格即可获得两个工具),它还将使移植到其他 GUI 环境变得更容易,并且更容易分发。

此 shellscript 打开一个文件选择器对话框并运行您的程序(创造性地命名为 yoursort)(未测试且 我不习惯编写 shell 脚本,我更喜欢其他脚本语言,我确信其他 stackoverflow贡献者可以找到很多需要改进的地方):

    #!/bin/sh

    FILE=`zenity --file-selection --title="Choose file to sort"`

    if [ $? -eq 0 ]; then
        yoursort -f "$FILE"
    fi

Zenity 是一个命令line 工具来创建简单的 GUI 对话框,它还包括一个进度对话框。

如果您想创建更高级的 GUI,请使用 tcl、Python 或 Ruby 等脚本语言。

Ruby GUI 工具包 Shoes 非常容易学习,它有一个 基于流程的布局引擎(有点像网页)。您可以使用 QT 做大部分事情(这似乎是其他答案中最受青睐的),或者至少可以完成 99% 的应用程序所需的所有事情。由于 Shoes 是构建在 GTK 上的层,因此它与 GTK 一样可移植,并且您可以使用已经为您安排的大部分繁重工作来扩展它(即,您不必直接处理 X 服务器、字体渲染或其他令人疲劳的工作)之类的东西)。


这篇文章的其余部分有点离题,但由于有人因为我建议使用脚本语言而降级了我,我觉得我必须激励为什么要编写 GUI
使用原始 C 或 C++ 几乎总是一个坏主意(除非您这样做的唯一目的是学习如何做)。

使用原始 C 或 C++ 创建 GUI 适合那些喜欢通过指甲钉钉子的人对于身体来说,当您必须更改应用程序的外观或添加新功能的那一天尤其痛苦。使用某种脚本语言,可以作为应用程序中的内置脚本语言 [tcl、lua、guile 和 pike 旨在轻松合并到您的应用程序中],或者围绕您创建的命令行工具创建 GUI 包装器。

还有很多 GUI 工具包(例如 GTK、QT)可以从某种 GUI 描述文件(如 XML 文件)加载 GUI:s。与编写和修改原始 C 或 C++ 相比,这些文件的编写和修改(或使用 GUI 构建器创建和修改)要容易得多。

如果速度很重要并且作为最后的手段,请使用某种高级语言为 GUI 生成 C 或 C++ 代码,它比编写原始代码更灵活。但是,如果您这样做,您仍然需要为第一个版本的 GUI 完成大部分痛苦的繁重工作,就像您用原始 C 或 C++ 编写它一样。 TCL、M4 和 Ruby 等语言擅长这种元编程。另一个最后的手段是编写自己的GUI加载器,然后您可以使GUI描述文件的语法更加专业,这将使其比GUI工具包中现成的GUI加载器更快(有时提供现成的 GUI 加载器不支持的 UI 小部件),这将让您更灵活地更改 GUI,然后用原始 C 或 C++ 对其进行编码。

附言。 QT 使用预处理器来减轻 C++ 中 GUI 编程的痛苦,但它仍然是非常低级的,使用起来仍然不必要的痛苦。

If sorting is the only thing your program does and if it is to be used in a unix-y environment, then I suggest making the program a command line tool and create a GUI wrapper in some script language to call that program. It will be simpler to code as well as more useful (you get two tools for the price of half a tool), it will also make it easier to port to other GUI environments as well as make it easier to distribute.

This shellscript opens a file-chooser dialog and run your program (creatively named yoursort) (not tested and I'm not used to writing shell scripts, I prefer other scripting languages, I'm sure other stackoverflow contributors can find a lot of things to improve upon):

    #!/bin/sh

    FILE=`zenity --file-selection --title="Choose file to sort"`

    if [ $? -eq 0 ]; then
        yoursort -f "$FILE"
    fi

Zenity is a command line tool to create simple GUI dialogues, it also includes a progress dialog.

If you want to create a more advanced GUI, use a scripting language like tcl, Python or Ruby.

The Ruby GUI toolkit Shoes is extremely easy to learn, it has a flow based layout engine (kinda like a web-page). You can do most things you can do with QT (which seems to be the favoured in other answers), or at least all the things you need for 99% of your applications. As Shoes is built as layer on GTK it is as portable as GTK and you can extend it with most of the grunt work already laid out for you (i.e. you don't have to deal directly with X servers, font rendering, or other fatiguing stuff like that).


The rest of this post is kind of of-topic, but since someone downgraded me because I suggested using a scripting language, I feel that I have to motivate why coding a GUI
in raw C or C++ is almost always a bad idea (unless you do it with the sole purpose of learning how to do it).

Using raw C or C++ to create a GUI is for people that enjoy driving nails through their body, it is especially painful the day you have to change the look of your application or add new functionality. Use some scripting language, either as a builtin scripting language within your application [tcl, lua, guile and pike is designed to be easy to incorporate into your application], or create a GUI wrapper around a command line tool that you create.

There is also a lot of GUI toolkits (e.g. GTK, QT) that can load GUI:s from some kind of GUI description files, like an XML file. Those files are a hell of a lot easier to write and modify (or create and modify with a GUI builder) then writing and modifying raw C or C++.

If speed is important and as a last resort, use some high level language to generate C or C++ code for your GUI, it is way more flexible to changes then writng raw code. But if you do this you still have to do most of the painful grunt work for the first version of the GUI, as you would if you would have written it in raw C or C++. Langauges like TCL, M4 and Ruby excel at this kind of meta programming. Another last resort is to write your own GUI loader, then you can make the syntax of the GUI description file more specialist and that will make it faster than the GUI loaders that come ready-made with GUI toolkits (and sometime provide UI widgets the ready-made GUI loaders don't support) and, again, this will give you more flexibility to change your GUI then coding them in raw C or C++.

PS. QT use a preprocessor to ease the pain of GUI programming in C++, but it is still very low level and still unnecessary painful to use.

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