PDA

View Full Version : Signal problem with QFileDialog



Eduardo Huerta
15th June 2018, 19:39
Hello everyone,
I'm doing a program to acquire data from an NI USB-6008, where the acquired data is saved in a .bin file. This file can be exported to a file that can be opened in excel. The problem I have is to open the window to locate the file to export, the window created by QFileDialog does not send me the signal issued by means of "fileSelected ()" to send the address of the file to be exported to a function for this purpose. The only way I can get the address of the file to be exported is to create the object type QFileDialog and use the function show (). I dont know becuase I can not emit the signal when the window generated it by means of getOpenFileName (). Thanks for your help.


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}
class QFileDialog;
class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void fileDialog();
void sentSignal(QString fileName);
private:
Ui::Widget *ui;
QFileDialog *fileDialogWin;
};
#endif // WIDGET_H



#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDebug>
#include <QPushButton>


Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QPushButton *button=new QPushButton("Open File",this);
connect(button, SIGNAL(clicked(bool)), this, SLOT(fileDialog()));
}

Widget::~Widget()
{
delete ui;
}

void Widget::fileDialog()//public slots.
{
fileDialogWin=new QFileDialog();
//fileDialogWin->getOpenFileName();//????????
fileDialogWin->show();
connect(fileDialogWin, SIGNAL(fileSelected(QString)), this, SLOT(sentSignal(QString)));
/*connect(fileDialogWin,&QFileDialog::fileSelected, [&](QString filename){
sentSignal(filename);
});*/
}

void Widget::sentSignal(QString fileName)//public slots.
{
qDebug() << "SIGNAL:" << fileName;
}

d_stranz
15th June 2018, 19:55
QFileDialog::getOpenFileName() is a static method of the QFileDialog class. It is irrelevant whether you create a QFileDialog instance; if you call a static method using a pointer to an instance of that class, the pointer is ignored. This is why your slot isn't called when you use getOpenFileName() - the pointer you use in the connect call is ignored, and the static method will not issue signals in any case.

If you want to received the signals emitted by QFileDialog, the correct method is this:



void Widget::fileDialog()
{
QFileDialog fileDialogWin( this );
connect( &fileDialogWin, SIGNAL( fileSelected(QString)), this, SLOT(sentSignal(QString)));
fileDialogWin.exec();
}


You will also need to add code that sets the initial directory, file filters, read / write mode, etc. before calling exec(); Look at the Qt examples if you don't know how.

Two things about this code: First, the QFileDialog instance is created on the stack and will automatically be deleted when the function exits. There is no need to create a QFileDialog instance using new() or to keep the pointer as a member variable in the Widget class. Second, QFileDialog::exec() is modal and blocks execution until the user clicks OK or Cancel on the dialog, so the method will not exit until the user has closed the file dialog.

If you do not understand what I said about static methods, do some research online or in your C++ books. It has nothing to do with Qt and everything to do with basic C++.

Eduardo Huerta
16th June 2018, 01:34
Thank you very much for your help. I solve the problem. regards