gpt4 book ai didi

c++ - 检索用户选择的结果作为调用 Qt 中的外部程序的输入?

转载 作者:行者123 更新时间:2023-11-28 05:34:28 25 4
gpt4 key购买 nike

我希望用户选择一个输入文件(例如 ~/MySampleTextFile.txt),然后选择一个输出目录。我希望程序将输入文件复制到输出目录。我想知道如何访问输入文件和输出目录作为下面 on_runButton_clicked() 函数的输入?感谢您的帮助。

UI of program

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>

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

主要.cpp:

#include "mainwindow.h"
#include <QApplication>

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

return a.exec();
}

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
# include <QFileDialog>
# include <QMessageBox>
# include <QString>
# include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->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;
}

主窗口.ui:

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

最佳答案

您可以像这样存储 filenameoutput_dir(在 MainWindow 的私有(private)部分):

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

...

private:
Ui::MainWindow *ui;
QString filename_;
Qstring output_dir_;
};

#endif // MAINWINDOW_H

然后在 on_runButton_clicked() 中像这样使用它:

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

...

QString MainWindow::on_inputFileNameButton_clicked()
{
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 command_to_call = "cp " + filename_ +" " + output_dir_; // copy the input_file_name to output_dir
QProcess::execute (command_to_call);
}

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

关于c++ - 检索用户选择的结果作为调用 Qt 中的外部程序的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com