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

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:18:25

问题


I would like to know how to list all folder in a directory in a listbox. I am using Visual C++.


回答1:


C++ is a language that does not specify any standard classes or functions for implementing graphical interfaces. 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?

So when you tag something "C++" or use that term, it refers only the language engine itself. 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?" is very open-ended. It depends on what toolkits you're choosing to use for the GUI, and for talking to the filesystem. 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/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. There's a way to enumerate files and directories with C++ using boost::filesystem. It's daunting for beginners, though:

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. 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.

Your other questions on SO are about Visual Studio and VB so I'm assuming you're using 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:

http://msdn.microsoft.com/en-us/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:

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:

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.




回答2:


There is an alternative one, free and open-source, called Nana C++ Library(http://stdex.sourceforge.net), a pure C++ GUI library.

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.



来源:https://stackoverflow.com/questions/8352139/how-to-populate-a-listbox-in-c-with-all-folders-in-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!