Retrieving result of user selections as input to call to external program in Qt?
<p>I want a user to select an input file (eg. ~/MySampleTextFile.txt) and then select an output directory. I want the program to then copy the input file to the output directory. I was wondering how I can access the input file and output directory as input to the on_runButton_clicked() function below? Thanks for your help.</p>

<p><a href="http://i.stack.imgur.com/3KYud.png" rel="nofollow">UI of program</a></p>

<p>mainwindow.h:</p>

<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QString&gt;

extern std::string selected_output_dir;
using namespace std;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    QString on_inputFileNameButton_clicked();

    void on_runButton_clicked();

    QString on_outputDirButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
</code></pre>

<p>main.cpp:</p>

<pre><code>#include "mainwindow.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
</code></pre>

<p>mainwindow.cpp:</p>

<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"
# include &lt;QFileDialog&gt;
# include &lt;QMessageBox&gt;
# include &lt;QString&gt;
# include &lt;QProcess&gt;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

QString MainWindow::on_inputFileNameButton_clicked()
{
    QString filename=QFileDialog::getOpenFileName(
                this,
                tr("Select file to analyze"),
                "~",
                "All files (*.*);; Text file (*.txt)"
                );
//    Qstring selected_output_dir = filename;
    QMessageBox::information(this,tr("File Name"),filename);
    return filename;
}

void MainWindow::on_runButton_clicked()
{
    QString input_file_name = "/Users/Shared/MySampleTextFile.txt";
    QMessageBox::information(this,tr("File Name"),input_file_name);
    QString output_dir = "/home/";
    QString command_to_call = "cp " + input_file_name +" " + output_dir; // copy the input_file_name to output_dir
    QProcess::execute (command_to_call);
}

QString MainWindow::on_outputDirButton_clicked()
{
    QString output_dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                "",
                                                QFileDialog::ShowDirsOnly
                                                | QFileDialog::DontResolveSymlinks);
    QMessageBox::information(this,tr("Output directory"),output_dir);
    return output_dir;
}
</code></pre>

<p>mainwindow.ui:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;ui version="4.0"&gt;
 &lt;class&gt;MainWindow&lt;/class&gt;
 &lt;widget class="QMainWindow" name="MainWindow"&gt;
  &lt;property name="geometry"&gt;
   &lt;rect&gt;
    &lt;x&gt;0&lt;/x&gt;
    &lt;y&gt;0&lt;/y&gt;
    &lt;width&gt;400&lt;/width&gt;
    &lt;height&gt;300&lt;/height&gt;
   &lt;/rect&gt;
  &lt;/property&gt;
  &lt;property name="windowTitle"&gt;
   &lt;string&gt;someProgramv0.1&lt;/string&gt;
  &lt;/property&gt;
  &lt;widget class="QWidget" name="centralWidget"&gt;
   &lt;widget class="QPushButton" name="runButton"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;150&lt;/x&gt;
      &lt;y&gt;160&lt;/y&gt;
      &lt;width&gt;115&lt;/width&gt;
      &lt;height&gt;32&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="text"&gt;
     &lt;string&gt;Run&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QLabel" name="mainProgramTitle"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;130&lt;/x&gt;
      &lt;y&gt;70&lt;/y&gt;
      &lt;width&gt;141&lt;/width&gt;
      &lt;height&gt;16&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="text"&gt;
     &lt;string&gt;&amp;lt;html&amp;gt;&amp;lt;head/&amp;gt;&amp;lt;body&amp;gt;&amp;lt;p&amp;gt;&amp;lt;span style=&amp;quot; font-size:14pt; font-weight:600; font-style:italic; color:#ff0000;&amp;quot;&amp;gt;SomeProgram v0.1&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;widget class="QWidget" name="layoutWidget"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;80&lt;/x&gt;
      &lt;y&gt;130&lt;/y&gt;
      &lt;width&gt;269&lt;/width&gt;
      &lt;height&gt;32&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;layout class="QHBoxLayout" name="horizontalLayout"&gt;
     &lt;item&gt;
      &lt;widget class="QPushButton" name="inputFileNameButton"&gt;
       &lt;property name="text"&gt;
        &lt;string&gt;Input File&lt;/string&gt;
       &lt;/property&gt;
      &lt;/widget&gt;
     &lt;/item&gt;
     &lt;item&gt;
      &lt;widget class="QPushButton" name="outputDirButton"&gt;
       &lt;property name="text"&gt;
        &lt;string&gt;Output Folder&lt;/string&gt;
       &lt;/property&gt;
      &lt;/widget&gt;
     &lt;/item&gt;
    &lt;/layout&gt;
   &lt;/widget&gt;
  &lt;/widget&gt;
  &lt;widget class="QToolBar" name="mainToolBar"&gt;
   &lt;attribute name="toolBarArea"&gt;
    &lt;enum&gt;TopToolBarArea&lt;/enum&gt;
   &lt;/attribute&gt;
   &lt;attribute name="toolBarBreak"&gt;
    &lt;bool&gt;false&lt;/bool&gt;
   &lt;/attribute&gt;
  &lt;/widget&gt;
  &lt;widget class="QMenuBar" name="menuBar"&gt;
   &lt;property name="geometry"&gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;400&lt;/width&gt;
     &lt;height&gt;22&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;widget class="QMenu" name="menu_File"&gt;
    &lt;property name="title"&gt;
     &lt;string&gt;&amp;amp;File&lt;/string&gt;
    &lt;/property&gt;
   &lt;/widget&gt;
   &lt;addaction name="menu_File"/&gt;
  &lt;/widget&gt;
  &lt;widget class="QStatusBar" name="statusBar"/&gt;
 &lt;/widget&gt;
 &lt;layoutdefault spacing="6" margin="11"/&gt;
 &lt;resources/&gt;
 &lt;connections/&gt;
&lt;/ui&gt;
</code></pre>