PDA

View Full Version : how to specify variable file name?



aurora
11th October 2011, 09:55
hello everyone....
i'm opening a file as shown below....
how can I specify variable file name......different time, user may give different file name.....
When i'm trying to change the following format, i got error....
Please help me


QFile file(":/input.txt");

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
ui->label_4->setText("FILE NAME:"+filename);
while (!file.atEnd())
{

//DOING REST OPERATION...
}

wysota
11th October 2011, 10:01
QString filename = ":/input.txt";
QFile file(filename);

ui->label_4->setText(QString("FILE NAME: %1").arg(filename));

aurora
11th October 2011, 10:10
but i'm getting error while opening variable name...

QFile file(":/input.txt");

if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

Here instead of ":/input.txt", i want to specify a variable. Is that possible?

ChrisW67
11th October 2011, 10:17
Did you read Wysota's example?

aurora
11th October 2011, 10:19
Did you read Wysota's example?

ya...i dont want to open ":/input.txt" every time....
it must be a variable....but when i changed that format ":/...." i got error...

john_god
11th October 2011, 10:35
Check the Qt examples svgviewer and svggenerator, if you're using windows they are in
C:\QtSDK\Examples\4.7\painting\svgviewer
and
C:\QtSDK\Examples\4.7\painting\svggenerator

ChrisW67
11th October 2011, 11:30
ya...i dont want to open ":/input.txt" every time....
it must be a variable....but when i changed that format ":/...." i got error...
filename is a variable. If changing it generated a error, presumably trying to open the file, it was probably because it no longer pointed at a valid file.

wysota
11th October 2011, 12:53
ya...i dont want to open ":/input.txt" every time....
it must be a variable....but when i changed that format ":/...." i got error...

Maybe your real question is "how do I allow the user to choose a fle to open?"

If so, then the answer is use QFileDialog::getOpenFileName().


QString filename = QFileDialog::getOpenFileName();

aurora
17th October 2011, 06:04
No....i want to open a file, that file name is entered by user without any path...and my program will search in specific directory.
Now i just confusing how can i read file name and search in directory?


Maybe your real question is "how do I allow the user to choose a fle to open?"

If so, then the answer is use QFileDialog::getOpenFileName().


QString filename = QFileDialog::getOpenFileName();

This is also fine...but how can i select multiple files....it is just allowing me to select single file at one time

ChrisW67
17th October 2011, 06:20
Did you read the friendly manual? QFileDialog::getOpenFileNames()

aurora
17th October 2011, 06:26
Did you read the friendly manual? QFileDialog::getOpenFileNames()

Thank u i got it...:)