我正在制作一个能够对我的电路板进行编程的应用程序。我想通过文件对话框选择文件并通过上传按钮上传带有所选文件的板。我有两个类:MyFileDialog 和 CommandProcess。我将上传按钮单击信号连接到信号映射器,并将其映射到 CommandProcess::startProcess 插槽,该插槽使用所选文件的路径执行进程,但如果我在程序启动时未指明路径,则路径不正确。选择文件后如何更新映射参数?
main.cpp 代码的一部分:
QObject *uploadButton = mainForm->findChild<QObject*>("uploadButton");
QSignalMapper mapper;
ConsoleProcess proc;
MyFileDialog mfd;
QObject::connect(fileButton, SIGNAL(clicked()), &mfd, SLOT(openMyFileDialog()));
QObject::connect(uploadButton, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(uploadButton, mfd.getFilename());
QObject::connect(&mapper, SIGNAL(mapped(const QString &)),&proc, SLOT(startProcess(const QString &)));
MyFileDialog 类:
public:
MyFileDialog();
QString getFilename();
private:
QFileDialog fd;
public slots:
void openMyFileDialog();
QString MyFileDialog::getFilename() {
return fd.getOpenFileName();
}
控制台进程类:
private:
QProcess p;
public:
ConsoleProcess();
public slots:
void startProcess(const QString &);
void ConsoleProcess::startProcess(const QString & path) {
p.setWorkingDirectory("C:/avrdude");
p.start("cmd.exe /C start avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"" + path + "\":a");
}