Selecting multiple files for reading
-
wrote on 1 Feb 2021, 14:04 last edited by
Hi everyone. I have a code script like:
QString fileName; fileName = QFileDialog::getOpenFileName(this, tr("choose"), "up.sakla", tr("choosen(*.up)")); if (fileName.isNull()) return; QFile file(fileName); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString str; qint32 a; in >> str >> a;
I want to select multiple files, when theselection windows comes. How can i do it? Is it possible?
-
@suslucoder said in Selecting multiple files for reading:
How can i do it?
Look at the documentation of QFileDialog::getOpenFileName and you will find a corresponding link to another function the the 'See also' section.
-
@suslucoder said in Selecting multiple files for reading:
How can i do it?
Look at the documentation of QFileDialog::getOpenFileName and you will find a corresponding link to another function the the 'See also' section.
wrote on 1 Feb 2021, 14:15 last edited by deleted286 2 Jan 2021, 14:17@Christian-Ehrlicher I convert my code into
``` QStringList fileName; fileName = QFileDialog::getOpenFileNames(this, tr("choose"), "up.sakla", tr("choosen(*.up)")); if (fileName.isEmpty()) return; QFile file(fileName); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString str; qint32 a; in >> str >> a;
but it gives me an error on the line, with saying
no matching constructor for initialization of QFileQFile file(fileName);
-
@Christian-Ehrlicher I convert my code into
``` QStringList fileName; fileName = QFileDialog::getOpenFileNames(this, tr("choose"), "up.sakla", tr("choosen(*.up)")); if (fileName.isEmpty()) return; QFile file(fileName); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString str; qint32 a; in >> str >> a;
but it gives me an error on the line, with saying
no matching constructor for initialization of QFileQFile file(fileName);
wrote on 1 Feb 2021, 14:18 last edited by JonB 2 Jan 2021, 14:30@suslucoder
ObviouslyQFile
can only handle opening one file at a time! You have to iterate through the list. And please rename yourfilename
tofilenames
orfilenameList
for everyone's sanity! -
@suslucoder
ObviouslyQFile
can only handle opening one file at a time! You have to iterate through the list. And please rename yourfilename
tofilenames
orfilenameList
for everyone's sanity!wrote on 1 Feb 2021, 14:21 last edited by@JonB said in Selecting multiple files for reading:
You have to iterate through the list.
What does it mean?
-
@JonB said in Selecting multiple files for reading:
You have to iterate through the list.
What does it mean?
wrote on 1 Feb 2021, 14:28 last edited by JonB 2 Jan 2021, 14:29@suslucoder
You receive a list of filenames. You have to go through one-by-one (that's called "iterating" or "iteration"), one file a time, doing whatever you want to do to each one withQFile
. I don't honestly see what else you would expect?! -
@suslucoder
You receive a list of filenames. You have to go through one-by-one (that's called "iterating" or "iteration"), one file a time, doing whatever you want to do to each one withQFile
. I don't honestly see what else you would expect?!wrote on 1 Feb 2021, 14:32 last edited by@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
-
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
Lifetime Qt Championwrote on 1 Feb 2021, 14:34 last edited by Christian Ehrlicher 2 Jan 2021, 14:36@suslucoder But even then you should know how to iterate over a container, sorry.
https://stackoverflow.com/questions/498455/best-way-to-iterate-through-a-container
-
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
Lifetime Qt Championwrote on 1 Feb 2021, 14:35 last edited by jsulm 2 Jan 2021, 14:36@suslucoder Well this is Qt forum, not basic programming one. That's why people can give somewhat harsh replies if you ask about basic programming stuff not related to Qt. This is something you should understand.
Iterating trough a list is one of the basic programming concepts. You should do some research for such basic stuff at your own before asking here. For example using Google you can find: https://stackoverflow.com/questions/16825376/qt-foreach-loop-ordering-vs-for-loop-for-qlist -
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
wrote on 1 Feb 2021, 14:37 last edited by JonB 2 Jan 2021, 14:38@suslucoder said in Selecting multiple files for reading:
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
I think I explained it very gently & clearly! And told you what you need to do, and explained what the term "iteration" is. So I'm sorry you feel like that, I believe I am one of the people here who is prepared to explain things the most! :) Maybe, if you can, don't take my words as harsh, because they are not intended like that! I'm a pussy cat really :-;
-
@suslucoder said in Selecting multiple files for reading:
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
I think I explained it very gently & clearly! And told you what you need to do, and explained what the term "iteration" is. So I'm sorry you feel like that, I believe I am one of the people here who is prepared to explain things the most! :) Maybe, if you can, don't take my words as harsh, because they are not intended like that! I'm a pussy cat really :-;
wrote on 1 Feb 2021, 14:42 last edited by@JonB Yes, after seeing the other answers, I understood that you answered my question very kindly. Thank you for your help
-
@suslucoder But even then you should know how to iterate over a container, sorry.
https://stackoverflow.com/questions/498455/best-way-to-iterate-through-a-container
wrote on 1 Feb 2021, 14:42 last edited by@Christian-Ehrlicher yes I don't know, is it a shame that I don't know?
-
@suslucoder Well this is Qt forum, not basic programming one. That's why people can give somewhat harsh replies if you ask about basic programming stuff not related to Qt. This is something you should understand.
Iterating trough a list is one of the basic programming concepts. You should do some research for such basic stuff at your own before asking here. For example using Google you can find: https://stackoverflow.com/questions/16825376/qt-foreach-loop-ordering-vs-for-loop-for-qlistwrote on 1 Feb 2021, 14:43 last edited by@jsulm Okey.
-
@Christian-Ehrlicher yes I don't know, is it a shame that I don't know?
@suslucoder Please start with basic c and c++ - everything else will not work out properly. And I'm pretty sure so will tell you the same.
-
@suslucoder said in Selecting multiple files for reading:
@JonB If I could think and do all of this at once, I wouldn't ask for help. You don't have to be this hard every time. I'm probably younger than you, and I'm making an effort to learn. It shouldn't be that hard to understand
I think I explained it very gently & clearly! And told you what you need to do, and explained what the term "iteration" is. So I'm sorry you feel like that, I believe I am one of the people here who is prepared to explain things the most! :) Maybe, if you can, don't take my words as harsh, because they are not intended like that! I'm a pussy cat really :-;
wrote on 2 Feb 2021, 12:10 last edited by@JonB I want to ask you something.
I did it in the way you said. Now i can select multiple files.
After selecting, i want to do some operations. But my code do it just for one file. The other files are not functional.Is there any way to do it like, do these for every different files.
I will share you my iteration, i hope it is correct:Q_UNUSED(b); QStringList fileNames; fileNames = QFileDialog::getOpenFileNames(nullptr, ("choose"), "up.sakla", ("choosen(*.up)")); for (auto xfile : fileNames) { QFile file (xfile); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString str; qint32 a; qDebug() << str << a; in >> str >> a;
-
@JonB I want to ask you something.
I did it in the way you said. Now i can select multiple files.
After selecting, i want to do some operations. But my code do it just for one file. The other files are not functional.Is there any way to do it like, do these for every different files.
I will share you my iteration, i hope it is correct:Q_UNUSED(b); QStringList fileNames; fileNames = QFileDialog::getOpenFileNames(nullptr, ("choose"), "up.sakla", ("choosen(*.up)")); for (auto xfile : fileNames) { QFile file (xfile); file.open(QIODevice::ReadOnly); QDataStream in(&file); // read the data serialized from the file QString str; qint32 a; qDebug() << str << a; in >> str >> a;
Lifetime Qt Championwrote on 2 Feb 2021, 12:18 last edited by jsulm 2 Feb 2021, 12:19@suslucoder So, did you select more than one file? How many entries do you have in fileNames? Did you use debugger to see what happens?
Addfor (auto xfile : fileNames) { qDebug() << xfile;
to see what happens. There are so many things you can do to find out what happens...
-
@suslucoder So, did you select more than one file? How many entries do you have in fileNames? Did you use debugger to see what happens?
Addfor (auto xfile : fileNames) { qDebug() << xfile;
to see what happens. There are so many things you can do to find out what happens...
wrote on 2 Feb 2021, 12:21 last edited by deleted286 2 Feb 2021, 12:22@jsulm said in Selecting multiple files for reading:
qDebug() << xfile;
Yes i select and i can see them. I want to do such a thing:
do some operation
for every file that i select -
@jsulm said in Selecting multiple files for reading:
qDebug() << xfile;
Yes i select and i can see them. I want to do such a thing:
do some operation
for every file that i select@suslucoder What do you think you print out to qDebug here:
QString str; qint32 a; qDebug() << str << a; in >> str >> a;
-
@suslucoder What do you think you print out to qDebug here:
QString str; qint32 a; qDebug() << str << a; in >> str >> a;
wrote on 2 Feb 2021, 12:23 last edited by@jsulm I wrote it for see the content of my file. Im asking another thing..
-
@jsulm I wrote it for see the content of my file. Im asking another thing..
@suslucoder Do you really think the order of the two last lines is correct?
8/23