Creating and using the very same directory for next savings
-
Hi,
At line 52, you have a forever loop that will create a new directory each time you call monslot since if the directory already exists you create a new one.
I would also recommend you to take a look at the layout related class to setup your widget rather that position every piece by hand.
Last point, please have a look at the Qt examples, you seem to have a mix of widgets created by hand, a ui built with designer, and you first constructor make me think you are not fully aware of what is going on.
Hope it helps
-
Hi,
Thank you for your reply.
You are right I am not always fully aware of what I am doing. But I try to improve my level of expertise day after day.And indeed in line 52 I have a forever-working loop.
And that is in fact the origine of my question.I supposed I have to add a test such as:
@
if (first_entry) { //created the subdirectory}
// then create the file and open it.
@
But I do not how to do it.
Thank you for your help -
Could you describe your workflow ?
i.e. :- user write something here
- user click there
- program should create thing here
etc...
That way it will be easier to come up with the right code
-
Hi,
Ok
User writes a number in lineEdit line and the corresponding company name in lineEdit line2.
Then user presses the button 'next'
the computer is supposed to create a directory named 'repertoire-1 ou 'repertoire-2 ou repertoire-3 ... according to what already exists.
Then in this directory (for example repertoire-3, assumming that repertoire-1 and repertoire-2 already exist) the computer is supposed to save the several files in this directory (each time the user hits the button 'next').
The files are named 'nom1, nom2,.... and each of these files contains line.text() (the client number) and line2.text(). (the company name)
quit button is to exit the button.
Thank you for your help -
In that case:
When do you really need to increment repertoire ? On the application start ? The first time the next button is pressed after the application has been started ?Also, why do you store these information in files ? Wouldn't a database be more suited ?
-
Hi,
Thank you for your reply.The directory named repertoire has to be incremented or created at the very first beginning. It could be done at the application start or when the next button is hit for the first time.
Indeed these informations could be saved in a database. But the files would bring more flexibility in their future use. And moreover I would be happy to learn a bit more of programming in overcoming this difficulty.
Thank you for your help -
Then:
Add a boolean variable (member of your class) that you check when you hit your next button i.e.:
@
if (!_firstClick) {
_repertoirePath = createNewRepertoire();
_firstClick = true;
}QFile nameFile(QString("%1/nom%2.txt").arg(_repertoirePath).arg(_nextFileNumber);
// Take a look at QTextStream to write your file
++_nextFileNumber;
@I encourage you think carefully about flexibility and future use. Having lots of numbered files in numbered directories might not be the best solution.
-
Hi,
Then I have to write:
@
fenetre::fenetre( bool firstClick,QLineEdit *line,QLineEdit *line2){
//initialise firstClick to false
firstClick=false;
@And inside fenetre::monSlot() , I have to insert the lines you wrote
Is that correct?
Thank you -
No,
This fenetre constructor and how you use it is wrong.
You already have line and line2 as class member (otherwise your code would not compile)
Add firstClick after the declaration of line and line2 and please, get a good C++ book, that will help you a lot.
-
Thank you for your help.
But it seems not to work. I certainly missed something.
When _firstClick is true , _repertoirePath=""@
//In fenetre.h
public:
bool _firstClick;
@@
// in main
fenetre Mafenetre;
Mafenetre._fitstClick=false;
Mafenetre.show();
@
@
void fenetre::monslot(){
.this->hide();
if (!_firstClick){
int n=1;
QDir lDir;
QString dossier="repertoire";
while (true){
dossier = "repertoire"+ qApp->tr("-%1").arg(n);
if ( !lDir.exists ( dossier )){
lDir.mkdir(dossier);
.break;
}
else
n += 1;
}
QString _repertoirePath=dossier+"\";
_firstClick=true;
}
.//-----------------------------------
QFile output(QString("%1/nom%2.txt").arg(_repertoirePath).arg(line.text());
//------------------------------------------------------
bool ok = output.open(QFile::WriteOnly|QFile::Text);
if (ok)
{..........}
@