通过qt中的clicked函数直接执行批处理 [英] Directly executing a batch through clicked function in qt
问题描述
所以我试图让我的按钮"直接执行一个批处理文件,这里重要的是我不希望它向我显示对话并让我选择路径,这就是我遇到的问题现在使用以下代码
So I'm trying to have my "button" directly execute a Batch file, important here is that I don't want it to show me a dialogue and make me chose the path, which is the problem I'm having right now with the following code
void MainWindow::on_pushButton_clicked()
{
QString filename=QFileDialog::getOpenFileName(
this,
tr("Open File"),
"C://",
"All files (*.*);;Text File (*.txt);;Music file (*.mp3)");
}
我认为这可能真的很简单,但我无法理解,我现在什至没有学习 C++,但我的老板让我创建超出我范围的东西(希望我为批处理文件并让它们交互),我想到了这种方法,它只是创建一个执行它的 GUI.
I think this is probably really simple, but i can't get it, I'm not even learning c++ at the moment but my boss asked me to create something out of my scope (wants me to create a GUI for a batch file and have them interact) and I thought of this approach, which is just creating a GUI that executes it.
我看过这个问题:要求使用 Qt 执行外部程序
但他们没有讨论如何将文件路径直接添加到代码中,或者我是否应该使用 Qprocess 以及如何使用,以及我是否可以通过单击"函数传递它.
but they don't talk about how the file path can directly be added into the code, or if I should even be using Qprocess and how, and if I can pass it through "clicked" function.
我真的很没经验,上面所有的代码都是我在网上的帮助下得到的,但我真的不知道如何使用c++编程所以有人可以向我展示如何将文件路径添加到代码中,假设它位于 C:\Users\name_goes_here\Downloads
I'm really inexperienced, all of the code above I got with the help of the internet, but I really don't know how to program using c++ so could someone please be kind enough to show me how a file path can be added to the code, assuming it's in C:\Users\name_goes_here\Downloads
我真的很感激:D
推荐答案
我建议将 QProcess 用于任何使用 Qt 的执行外部程序".
I'd recommend using QProcess for anything "execute external program" with Qt.
你可以这样做:
void MainWindow::on_pushButton_clicked()
{
QProcess process;
process.start("C:/Users/name_goes_here/Downloads/yourfile.bat");
process.waitForFinished(); // Assuming that you do want to wait for it to finish before the code execution resumes
}
注意路径中的/".只有 Windows 使用混乱的\"进行路径分隔,这将要求您在 C++ 中的任何字符串中编写 "C:\\Users\\.."
因为\"需要转义.
幸运的是,Qt 使用/"作为通用分隔符,并根据需要将其转换为操作系统需要的任何内容.所以你应该在使用 Qt 时使用/".
Note the "/" in the path. Only Windows uses the messed up "\" for path separation, which would require you to write "C:\\Users\\.."
in any string in C++ as "\" needs to be escaped.
Luckily, Qt uses "/" as the universal separator and translates it to whatever the OS needs as required. So you should just use "/" whenever working with Qt.
这来自 Qt 文档:
Qt 使用/"作为通用目录分隔符,就像/"在 URL 中用作路径分隔符一样.如果您总是使用/"作为目录分隔符,Qt 将转换您的路径以符合底层操作系统.
Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.
最后,如果您不知道如何用 C++ 编写代码,您是否应该先学习这一点,而不是尝试从像 Qt 这样复杂的库中执行批处理文件?听起来你试图一次做太多新的事情.
And finally, if you don't know how to code in C++, shouldn't you be learning that first instead of trying to execute batch files from within a library as complex as Qt? Sounds like you're trying to do too many new things at once.
这篇关于通过qt中的clicked函数直接执行批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!