用文件管理器创建空目录的方法
我正在 Linux 上使用 Qt Creator 进行编译。我已经使用 Qt Designer 完成了 GUI 的基础知识。现在我需要知道是否有一种方法可以打开文件管理器并让我创建一个以我命名的目录?
我发现了这个:(
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
参见http://doc.qt.io/archives /qt-4.7/qfiledialog.html#directory)
我用一个方法连接了一个 QPushButton 并将上面的内容写入其中。 问题是: 当我按下按钮时,文件管理器甚至没有打开。此外,这只适用于创建不存在的文件(请参阅 AnyFile),但我需要创建一个不存在的目录。
我也知道创建目录的可能性:
QDir("/home/name").mkdir("NewDirectory");
但这并不能满足我的要求,因为名称和目录始终相同。
I am compiling on Linux with Qt Creator. I already finished the basics of my GUI with the Qt Designer. Now I need to know if there is a method available that opens the file manager and let me create a directory named by me?
I found this:
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
(see http://doc.qt.io/archives/qt-4.7/qfiledialog.html#directory)
I have connected a QPushButton with a method and wrote the above into it.
Problems are: The file manager does not even open when I push the Button. Furthermore this should only work for creating not existent files (see AnyFile), but I need to create a not existing directory.
I also know this possibility for creating a directory:
QDir("/home/name").mkdir("NewDirectory");
But this does not fulfill my demands since the name and the directory are always the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是为了解决不存在的文件夹/文件的问题:
您需要一些变量:
在按钮调用的插槽中,您必须调整参数,以便它们适合您的需求。例如,您可以使用通过按钮传递的参数或由某些内部事件操纵的参数。在这种情况下,您当然可以通过使用变量动态创建文件夹:
另一种对用户更友好的方法是通过允许用户输入自定义路径的行/文本编辑元素从 UI 获取值。您可以通过 UI 类读取这些值(这里我只使用标准 QT 名称 ui)。如果您选择以不同的方式命名,则名称 lineEdit 在您的情况下可能会有所不同。但您仍然可以访问数据并在底层代码中动态使用 id。确保正确连接您的信号和方法。
这是另一种方法,因为根据我的理解,您想要使用某种文件管理器:
您实际上可以做的是创建自己的文件管理器小部件。 QDir 方法基本上为您提供了显示文件夹所需的所有信息,包括。一些基于项目的 QTreeWidget 中的文件,导航也将是一项简单的任务,因为您可以使用 QTreeWidget 的信号和槽进行导航,您只需用文件夹信息填充树即可。创建新文件夹只需用户交互(鼠标右键或单击自定义文件管理器小部件中的 QPushButton“创建文件夹”)
Here's for solving the problem with the nonexistent folders / files:
You need some Variables:
and in the Slot that is being called by your button you'll have to adjust the parameters so that they suit your demands, for. e.g. you may use the parameters being handed down through the button or being manipulated by some internal events. In that case you could of course create folders dynamically through using the variables:
Another, more user friendly way would be to fetch values from the UI through a line/text edit element which allows the user to enter a custom path. You can read the values through the UI class ( here I just used the standard QT name ui). The name lineEdit can be different in your case if you chose to name it differently. But you can access the data nevertheless and use id dynamically in your underlying Code. Make sure to connect your signals and methods properly.
Here's another approach, since from my understanding you want to use some sort of File Manager:
What you actually could do would be to create your own File Manager Widget. The QDir Method basically gives you all the information you need for displaying a folder incl. files in some
Item-based QTreeWidget
, Navigation through that would also be an easy task since you can use the signals and slots of the QTreeWidget to navigate, you would just have to fill the Tree with your folder Information. Creating a new folder would then only be a user interaction(right mouse button or maybe clicking a QPushButton "Create Folder" in your custom File Manager Widget)