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()
{
connect( &fileDialogWin,
SIGNAL( fileSelected
(QString)),
this,
SLOT(sentSignal
(QString)));
fileDialogWin.exec();
}
void Widget::fileDialog()
{
QFileDialog fileDialogWin( this );
connect( &fileDialogWin, SIGNAL( fileSelected(QString)), this, SLOT(sentSignal(QString)));
fileDialogWin.exec();
}
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++.
Bookmarks