PDA

View Full Version : Verify data in a dialog



admkrk
6th July 2014, 22:28
I have a dialog that creates a file and writes some data to it. The problem is that if the file already exists, it is overwritten before I catch the original file existing. I have the file names stored in a QStringList and I think I need to pass the list to the dialog to verify if it exists before it gets written. Here is what I have currently to open the dialog

void gui::on_actionCreate_triggered()
{
CreateDialog *create = new CreateDialog(this);
create->show();

connect(create, SIGNAL(nameAdded(QString)), this, SLOT(vehicle_name_entered(QString)));
...
}
Then I check if a name is entered and open a new dialog if it is empty. This also where I believe I need to check if it already exists.

void CreateDialog::on_create_vehicle_btn_clicked()
{
// Check if name is entered.
if(ui->vehicle_name_dialog_txtbox->text() == "")
{
NameDialog *name = new NameDialog(this);
name->show();

connect(name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
return;
}
...
emit nameAdded(ui->vehicle_name_dialog_txtbox->text());
...

this->close();
}
Or maybe in NameDialog?

void NameDialog::on_ok_btn_clicked()
{
emit name_Added(ui->name_txtbox->text());

this->close();
}
Currently I have the checking done here where I add it to the file that stores the QStringList. The new dialog here is basically the same as the last one and simply asks for another name and says the old file will be over written otherwise.

void gui::vehicle_name_entered(QString filename)
{
read_vehicle_file();
if(!filename.isEmpty())
{
file_label->setText(filename);
// Check if exists,
for(int i = 0; i < vehicle.size(); ++i)
{
if(filename == vehicle[i])
{
NewNameDialog *new_name = new NewNameDialog(this);
new_name->show();

connect(new_name, SIGNAL(name_Added(QString)), this, SLOT(new_vehicle_name_entered(QString)));
return;
}
}
}

vehicle << filename;

QFile file("vehicleList.txt");
if(!file.open(QFile::Append | QFile::Text))
{
return;
}
QTextStream out(& file);
out << filename << "\n";

file.close();
}
I have pretty much confused myself trying different approaches so I am hoping someone else can straighten me out and get me headed back in the right direction.

anda_skoa
7th July 2014, 09:11
Can you be a bit more precise what you want to improve?

Do you want to check before overwriting or do you want to check when the name is entered?

Cheers,
_

admkrk
7th July 2014, 18:44
I need to give the user a chance to change the name before overwriting.

Originally I had this as a two step process, entering the name and entering the initial data that created the file. That was causing a crash if the user did not do both steps and later tried to open the file. Combining them fixed the crash, but now it has the overwriting problem.

I think the best solution would be to move the checking to CreateDialog where the name is entered. To do that I somehow need to make the dialog aware of the QStringList vehicle so it can check against the list of names. I hope that makes better sense now.


<edit>
I finally managed to find what I was doing wrong when I tried passing the QStringList in the constructor and changed the dialog to this

void CreateDialog::on_create_vehicle_btn_clicked()
{
// Check if name is entered.
if(ui->vehicle_name_dialog_txtbox->text() == "")
{
NameDialog *name = new NameDialog(this);
name->show();

connect(name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
return;
}

for(int i = 0; i < vehicle.size(); ++i)
{
if(ui->vehicle_name_dialog_txtbox->text() == vehicle[i])
{
NewNameDialog *new_name = new NewNameDialog(this);
new_name->show();

connect(new_name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
return;
}
}

...
emit nameAdded(ui->vehicle_name_dialog_txtbox->text());
...

this->close();
}
It is still overwriting the file though and now completely ignoring the new name.

anda_skoa
7th July 2014, 21:39
If you have a list of used names and want to prevent the user from entering these, then pass them to the dialog, e.g. in the constructor or via a setter method.

If you want to check prior to open the file for writing, see QFile::exists().

Cheers,
_

admkrk
7th July 2014, 22:21
Passing it in the constructor worked. The problem I had earlier was twofold, first I did not remove the check the in the main window(that is why it was ignoring the new name), then I was sending an empty list so it was not finding a match. It all seems to be working now, Thank you!