0

I'm making an application which will be able to program my board. I would like to select file via file dialog and upload board with selected file by upload button. I have two class to it: MyFileDialog and CommandProcess. I connected upload button clicked signal to signal mapper and i mapped it to CommandProcess::startProcess slot, which executes process with path of selected file but the path is incorrect if I don't indicate it on start of program. How can I update mapping parameter after i choose file?

Part of main.cpp code:

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 class:

public:
    MyFileDialog();
    QString getFilename();

private:
    QFileDialog fd; 

public slots:
    void openMyFileDialog();

QString MyFileDialog::getFilename()   {
    return fd.getOpenFileName();
}

ConsoleProcess class:

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");
}
sochinho
  • 87
  • 1
  • 6
  • Are you sure you need this `QSignalMapper`? You can open fileDialog, chech if file exist and call `Proc::startProcess()` with file path argument – t3ft3l--i Jul 27 '15 at 12:46
  • 1
    Or another way - to store path to chosed file at `MyFileDialog` class member – t3ft3l--i Jul 27 '15 at 12:49
  • It may be helpful, should I store path after upload button clicked signal? – sochinho Jul 27 '15 at 12:55
  • 1
    you can add slot at `MyFileDialog` for reset file path and connect signal `clicked()` of upload button with this slot. – t3ft3l--i Jul 27 '15 at 12:57
  • Ok I can connect reset to this button but I don't know what I can store path without `QSignalMapper`. – sochinho Jul 27 '15 at 13:09

1 Answers1

1

You can create 2 slots at your MainWindow class and QString member for storing path to file like this:

class MainWindow : public QMainWindow, private Ui::MainWindow
{
    Q_OBJECT

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

public slots:
    void choseSlot();
    void uploadSlot();

private:
    QString m_file;

};

Create connections at your constructor class and initialize m_file variable:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), m_file(QString())
{
    setupUi(this);

    connect(choseFile,  SIGNAL(clicked(bool)), this, SLOT(choseSlot()));
    connect(uploadFile, SIGNAL(clicked(bool)), this, SLOT(uploadSlot()));
}

Then time to realize described slots. Let's get QFileDialog::getOpenFileName static method result:

void MainWindow::choseSlot()
{
    m_file = QFileDialog::getOpenFileName(this, tr("Open file"), QDir::currentPath(), tr("Some Files (*.a *.b *.c)"));
}

And if file already chosen at upload slot we can tranfer them or do whatever you want:

void MainWindow::uploadSlot()
{
    if (!m_file.isEmpty()) {
        qDebug() << Q_FUNC_INFO << m_file;
        QProcess *prc = new QProcess;
        connect(prc, SIGNAL(finished(int)), prc, SLOT(deleteLater()));
        prc->setWorkingDirectory("C:/avrdude");
        prc->start("cmd.exe /C start avrdude.exe -c breakout -P ft0 -p m2560 -U flash:w:\"" + m_file + "\":a");
    }
}
t3ft3l--i
  • 1,372
  • 1
  • 14
  • 21
  • Thanks a lot for answer! It's my first app in QT and I tried to integrate QML and C++. You showed me the simpler way ;) – sochinho Jul 28 '15 at 08:15