c++qtsignals-slotsqfiledialogqsignalmapper

QSignalMapper does not update parameter after choosing file


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");
}

Solution

  • 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");
        }
    }