PDA

View Full Version : Getting filename by QFileDialog



Rajeshsan
21st January 2010, 10:24
Hi to all......
I want to get filename using QFileDialog, Filename should be in String( not StringList). I'm posting a code here. i need to get file in LineEdit.

Mainwindow.cpp
Void Mainwindow::XXXX
{
QFileDialog filedia;

filedia.resize(200,320);
filedia.exec();

filename=filedia.XXXXXXXXXXXXXXXXXXXXXXXXXXXX

filedia.close();
if(filename=="")
{
QMessageBox msg;
msg.setText("no input file specified");
msg.exec();
return;
}
le->setText(filename);

}
I'm getting Filedialog of size 240x320.But if i select any file the Path is not get set in LineEdit, Further this LineEdit string will be accesed for transfer from serial port. PLz suggest me what must be the option to be in XXXXXXXXXXXXXXXXXXXX or someother fuction should be called for this..

earlier i used
filename = filedia.getOpenFileName(this,tr("Open Image"), "D:/", tr("FILES(*.txt *.bmp *.wmv *.doc *.pdf *.jpg *.xls *.exe)"));

this was taking the file source path in line edit. But The problem was size of FileDialog it was too big, but i want to port in 240x320 Board. So plz suggest to get file name..
Thanking You.

yogeshm02
21st January 2010, 10:36
Are not you better off using QFileDialog::getOpenFileName() (http://qt.nokia.com/doc/4.6/qfiledialog.html#getOpenFileName)?
filename = QFileDialog::getOpenFileName(this);
EDIT: Sorry, did not read your entire post. You can go about this like:
Mainwindow.cpp
Void Mainwindow::XXXX
{
QFileDialog filedia;
filedia.

filedia.resize(200,320);
filedia.exec();

filename=filedia.XXXXXXXXXXXXXXXXXXXXXXXXXXXX

filedia.close();
if(filename.count() == 0 || filename.at(0).isEmpty())//Not sure if second condition is necessary, you can check it yourself, i'm currently away from my PC
{
QMessageBox msg;
msg.setText("no input file specified");
msg.exec();
return;
}
le->setText(filename.at(0));

}