Results 1 to 3 of 3

Thread: Signal problem with QFileDialog

  1. #1
    Join Date
    Jun 2018
    Posts
    31
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Signal problem with QFileDialog

    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.

    http://doc.qt.io/qt-4.8/
    #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


    http://doc.qt.io/qt-4.8/
    #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;
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Signal problem with QFileDialog

    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:

    Qt Code:
    1. void Widget::fileDialog()
    2. {
    3. QFileDialog fileDialogWin( this );
    4. connect( &fileDialogWin, SIGNAL( fileSelected(QString)), this, SLOT(sentSignal(QString)));
    5. fileDialogWin.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

    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++.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Eduardo Huerta (16th June 2018)

  4. #3
    Join Date
    Jun 2018
    Posts
    31
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Signal problem with QFileDialog

    Thank you very much for your help. I solve the problem. regards

Similar Threads

  1. Replies: 2
    Last Post: 3rd May 2011, 21:22
  2. Problem with QFileDialog
    By mobucl in forum Qt Programming
    Replies: 7
    Last Post: 2nd May 2011, 15:46
  3. QFileDialog Signal / Class Slot issue
    By Wasabi in forum Newbie
    Replies: 5
    Last Post: 8th August 2010, 00:48
  4. QFileDialog problem
    By jpoz in forum Newbie
    Replies: 2
    Last Post: 29th November 2008, 12:02
  5. Problem in the QFileDialog
    By joseph in forum Qt Programming
    Replies: 11
    Last Post: 7th December 2006, 10:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.