Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

c++ - 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)");
    }

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.

I've looked at this question: asked to execute an external program with Qt

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.

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 ame_goes_hereDownloads

I'd really appreciate it :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'd recommend using QProcess for anything "execute external program" with Qt.

You could do it like this:

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
}

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.

This is from the Qt documentation:

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...