PDA

View Full Version : Processo Ok or Cancel in a QFileDialog



gt.beta2
19th February 2009, 23:56
Hi

I'm using this function to get a file name.

void class::function(){
QFileDialog::Options options;
QString selectedFilter;
QString fileName = QFileDialog::getOpenFileName(this,
tr("QFileDialog::getOpenFileName()"),
lineEdit->text(),
tr("PNG image format (*.png)"),
&selectedFilter,
options);
if (!fileName.isEmpty())
lineEdit->setText(fileName);
}

How can i know if the user pressed Ok or Cancel?

Thanks

seneca
20th February 2009, 09:08
Basically user can only press OK when a filename is selected.

So filename not empty -> user pressed ok.

gt.beta2
20th February 2009, 13:08
But imagine it wad a Dialog made by me ... how to teste if the ok was the button presed?

rexi
20th February 2009, 13:39
Use the QDialog::accept() and QDialog::reject() slots by connecting your buttons to these slots. Then you can find out what the user clicked by calling QDialog::result().

gt.beta2
20th February 2009, 15:04
So the same thing is valid for the initial question about the QFileDialog?
Instead of testing if the field is empty i can test the accept or reject like said above?

rexi
20th February 2009, 16:49
Yes, but then you must use a file dialog instance. It won't work when using the file dialog's static methods.

gt.beta2
20th February 2009, 17:02
OK ... now i managed to know if the dialog was terminated with OK or Cancel.

And let me make the last newbie question for today;)

Imagine a dialog with :

lineEdit
cancelButton
pushButton


I call this dialog from the main application and the i want to use the contents of lineEdit there. Let's sai ... make a copy to lineEdit_2.

How is this managed?

Thanks

rexi
20th February 2009, 18:29
Simply add a method to your dialog that returns the contents of the lineedit. Whenever you need the value, call that method. It's the same principle as with QFileDialog: when you need to know what files were selected, you call QFileDialog::selectedFiles() ;)

gt.beta2
20th February 2009, 18:53
:o
ofcourse:rolleyes: