简体   繁体   English

如何用目录中的所有文件夹填充C ++中的列表框

[英]How to populate a Listbox in C++ with all folders in a directory

I would like to know how to list all folder in a directory in a listbox. 我想知道如何在列表框中列出目录中的所有文件夹。 I am using Visual C++. 我正在使用Visual C ++。

C++ is a language that does not specify any standard classes or functions for implementing graphical interfaces. C ++是一种语言,未指定用于实现图形界面的任何标准类或函数。 It also doesn't provide a standard way of getting lists of files and directories from the operating system!!! 它还没有提供从操作系统获取文件和目录列表的标准方法! In fact...for a long time there wasn't even a standard for string classes, and every project chose different incompatible strings. 实际上……很长时间以来,甚至没有针对字符串类的标准,并且每个项目都选择了不同的不兼容字符串。

Note: Even though there's now a standard string class, the fragmented legacy continues: Why is there a different string class in every C++ platform out there? 注意:即使现在有了一个标准的string类,但支离破碎的传统仍在继续: 为什么每个C ++平台中都存在一个不同的字符串类?

So when you tag something "C++" or use that term, it refers only the language engine itself. 因此,当您标记“ C ++”或使用该术语时,它仅指语言引擎本身。 As limited as that may sound, one of the great strengths is that this engine is so (relatively) powerful that you don't need to build things in to make them efficient or have a deep coding interface. 尽管听起来很有限,但是它的强大功能之一是(相对)强大,您无需内置任何东西即可使它们高效或具有深层的编码接口。 Users of the language have nearly as much power to create cool language features as if they were able to modify the compiler itself. 使用该语言的用户几乎可以创建酷炫的语言功能,就像他们能够修改编译器本身一样。

It's a steep hill to climb for beginners, though. 不过,对于初学者来说,这是一个陡峭的小山。


All of that means that a question like "How to populate a Listbox in C++ with all folders in a directory?" 所有这些都意味着一个问题,例如“如何用目录中的所有文件夹填充C ++中的列表框?” is very open-ended. 非常开放。 It depends on what toolkits you're choosing to use for the GUI, and for talking to the filesystem. 这取决于您选择用于GUI以及与文件系统对话的工具包。 Some toolkits are "big" and offer comprehensive classes to cover both areas: 一些工具包是“大型”的,并提供涵盖这两个领域的综合类:

http://doc.qt.nokia.com/latest/qdir.html#navigation-and-directory-operations http://doc.qt.nokia.com/latest/qdir.html#navigation-and-directory-operations

http://doc.qt.nokia.com/latest/itemviews-dirview.html http://doc.qt.nokia.com/latest/itemviews-dirview.html

Other libraries are more narrow and provide just one function or another. 其他库则更狭窄,仅提供一个或另一个功能。 For instance, "boost" is a set of almost standard libraries that haven't yet made it into the C++ spec. 例如,“ boost”是一组几乎尚未纳入C ++规范的标准库。 There's a way to enumerate files and directories with C++ using boost::filesystem . 有一种方法可以使用boost::filesystem用C ++枚举文件和目录。 It's daunting for beginners, though: 但是,对于初学者来说这是艰巨的:

http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/tutorial.html http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/tutorial.html

If you're willing to chain yourself to a particular operating system or implementation--such as make calls to functions only available on Windows, or only on Linux under GTK, or only on MacOS--then you have access to what that platform+toolkit provides. 如果您愿意将自己链接到特定的操作系统或实现方式(例如仅在Windows上或在GTK下仅在Linux上或仅在MacOS上进行对函数的调用),则可以访问该平台+工具包提供。 But because C++ is platform-independent, once you cross that line you're no longer programming in "just C++" and your question and tags on StackOverflow need to clarify what choices you've made. 但是由于C ++与平台无关,所以一旦越过这一行,您就不再使用“仅C ++”进行编程,并且StackOverflow上的问题和标签需要阐明您所做的选择。

Your other questions on SO are about Visual Studio and VB so I'm assuming you're using Windows. 关于SO的其他问题是关于Visual Studio和VB的,因此我假设您使用的是Windows。


One related topic you should be aware of are "common dialogs". 您应该注意的一个相关主题是“常见对话框”。 These are conveniences provided so that everyone doesn't have to write their own "File->Open" logic, or color picker, or search dialog. 提供这些便利是为了使每个人都不必编写自己的“文件->打开”逻辑,颜色选择器或搜索对话框。 Microsoft has some of them defined on Windows: Microsoft在Windows上定义了其中的一些:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646960(v=VS.85).aspx http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms646960(v=VS.85).aspx

So if picking a file, files, directory, or directories is the simple purpose of some code...those save you the trouble. 因此,如果选择一个文件,多个文件,一个目录或多个目录只是某些代码的简单目的……那可以节省您的麻烦。 Qt has similar things: Qt有类似的东西:

http://doc.qt.nokia.com/stable/qfiledialog.html#details http://doc.qt.nokia.com/stable/qfiledialog.html#details


You can edit your question to add more about your purposes. 您可以编辑问题以添加更多有关您的目的的信息。 If you are curious about C++ and just want to dive in and have a well-documented set of functionality that will work on Windows, Mac, or Linux... consider trying Qt Creator: 如果您对C ++感到好奇,并且只想深入研究并拥有一套可以在Windows,Mac或Linux上正常使用的功能完备的功能,请考虑尝试Qt Creator:

http://www.qt.io/ide/ http://www.qt.io/ide/

Microsoft has really hinged their strategy on .NET and C#, so at least right now you'll stand on firmer ground as a C++ GUI programmer if you go with Qt. Microsoft确实将他们的策略取决于.NET和C#,所以至少现在,如果您选择Qt,您将在C ++ GUI程序员的立场上站稳脚跟。

There is an alternative one, free and open-source, called Nana C++ Library(http://stdex.sourceforge.net), a pure C++ GUI library. 还有另一种免费的开源资源,称为Nana C ++库(http://stdex.sourceforge.net),这是一个纯C ++ GUI库。

There is a tutorial to populate a treebox with folders. 有一个教程用文件夹填充树框。

The status of the library is active, and it is updated monthly. 库的状态为活动,并且每月更新一次。 This is a new library, it would be a choice for your hobby project. 这是一个新图书馆,将是您爱好项目的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM