【发布时间】:2020-02-21 14:29:37
【问题描述】:
已经从其他脚本的 sn-ps 中组合了一些脚本。我正在尝试为解密备份的python脚本(HHDB.py)创建一个GUI。此脚本需要密码、加密文件所在文件夹的位置以及写入加密文件的目的地等参数。
使用 QT Creator 制作了一个漂亮的 gui,带有一个按钮可以浏览到包含加密文件的文件夹。它的路径显示在文本编辑字段中。 目的地也一样。 然后有一个字段可以输入已知密码。 最后有一个按钮可以运行 HHBD 脚本,参数为密码、起点和终点。
由于我不知道哪里出了问题,并且调试器(不再)给出任何错误,我需要一个程序员的眼睛来指出我的(许多)缺陷。
感谢您查看下面的代码:
谷歌搜索了很多,但许多发现的解决方案都给出了调试、查找和替换的错误:-(
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QString>
#include <QLabel>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString backup_dir = QFileDialog::getExistingDirectory(this,tr("Choose HiSuite Backup Folder"), QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui->textEdit->setPlainText(backup_dir.replace('/','\\'));
}
void MainWindow::on_pushButton_2_clicked()
{
QString destination_dir = QFileDialog::getExistingDirectory(this,tr("Choose Destination Folder"), QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
ui->textEdit_2->setPlainText(destination_dir.replace('/','\\'));
}
void MainWindow::on_pushButton_3_clicked()
{
QString passwordstring = ui->password->toPlainText();
QProcess process;
QString scriptFile = QCoreApplication::applicationDirPath() + "./HHBD.py";
QString pythonCommand = "python " + scriptFile +
" passwordstring" +
" backup_dir.replace('/','\\')" +
" destination_dir.replace('/','\\')";
printf("PyCommand: %s\n", pythonCommand.toStdString().c_str());
process.start (pythonCommand);
}
我想推送“pushButton_3”脚本执行带有给定参数的 HHBD.py 脚本,例如“python HHBD.py 123456789 C:\test\ c:\dest”,其中“123456789”来自密码字段, “C:\test\”来自backup_dir 字段,“C:\dest”来自destination_dir 字段
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
主窗口.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>395</width>
<height>303</height>
</rect>
</property>
<property name="windowTitle">
<string>Huawei HiSuite Backup Decrypter</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>J:/Downloads_2/huawei.png</normaloff>J:/Downloads_2/huawei.png</iconset>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Select HiSuite Backup Folder</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>381</width>
<height>23</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>10</x>
<y>70</y>
<width>151</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Select Destination Folder</string>
</property>
</widget>
<widget class="QTextEdit" name="textEdit_2">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>381</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>150</y>
<width>381</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="lineWidth">
<number>2</number>
</property>
<property name="text">
<string>Type here the password given during the creation of the backup of the mobile device with Huawei HiSuite.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
<widget class="QTextEdit" name="password">
<property name="geometry">
<rect>
<x>10</x>
<y>190</y>
<width>191</width>
<height>21</height>
</rect>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>10</x>
<y>220</y>
<width>151</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Decrypt HiSuite Backup</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>270</x>
<y>170</y>
<width>141</width>
<height>111</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>J:/Downloads_2/Aantekening 2019-10-24 141833.png</pixmap>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
除了
"...everything goes wrong"之外,您还没有真正描述过您遇到的问题。话虽如此,一个明显的问题是process在MainWindow::on_pushButton_3_clicked中被声明为局部变量,因此它将超出范围并在调用process.start(pythonCommand)后立即调用其析构函数。 -
嘿@G.M.,作为一名非程序员/编码员,而且还不是python 和QT Creator 的新手,我在你的回复中迷失了方向。澄清“一切”是我没有得到我想要的结果。除非“QProcess: Destroyed while process ("python") is still running”,否则什么都不会发生。我刚刚注意到。你能用noob语言解释一下什么是错的,为什么?
-
第二眼看去:
" backup_dir.replace('/','\\')"和" destination_dir.replace('/','\\')"是常量字符串,按字面意思传递。相反,您可以使用例如从ui->textEdit和ui->textEdit2中提取字符串。 -
谢夫怎么说。我也总是使用 start() 重载将参数作为 QStringList,因为它避免了引用问题
-
@Scheff:编辑了标签,我做了“backup_dir.replace('/','\\')”和“destination_dir.replace('/','\\')”以 windows 格式显示文件夹路径。我猜 HHBD.py 脚本需要 windows 格式的参数?