Results 1 to 5 of 5

Thread: Verify data in a dialog

  1. #1
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Verify data in a dialog

    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
    Qt Code:
    1. void gui::on_actionCreate_triggered()
    2. {
    3. CreateDialog *create = new CreateDialog(this);
    4. create->show();
    5.  
    6. connect(create, SIGNAL(nameAdded(QString)), this, SLOT(vehicle_name_entered(QString)));
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. void CreateDialog::on_create_vehicle_btn_clicked()
    2. {
    3. // Check if name is entered.
    4. if(ui->vehicle_name_dialog_txtbox->text() == "")
    5. {
    6. NameDialog *name = new NameDialog(this);
    7. name->show();
    8.  
    9. connect(name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
    10. return;
    11. }
    12. ...
    13. emit nameAdded(ui->vehicle_name_dialog_txtbox->text());
    14. ...
    15.  
    16. this->close();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Or maybe in NameDialog?
    Qt Code:
    1. void NameDialog::on_ok_btn_clicked()
    2. {
    3. emit name_Added(ui->name_txtbox->text());
    4.  
    5. this->close();
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. void gui::vehicle_name_entered(QString filename)
    2. {
    3. read_vehicle_file();
    4. if(!filename.isEmpty())
    5. {
    6. file_label->setText(filename);
    7. // Check if exists,
    8. for(int i = 0; i < vehicle.size(); ++i)
    9. {
    10. if(filename == vehicle[i])
    11. {
    12. NewNameDialog *new_name = new NewNameDialog(this);
    13. new_name->show();
    14.  
    15. connect(new_name, SIGNAL(name_Added(QString)), this, SLOT(new_vehicle_name_entered(QString)));
    16. return;
    17. }
    18. }
    19. }
    20.  
    21. vehicle << filename;
    22.  
    23. QFile file("vehicleList.txt");
    24. if(!file.open(QFile::Append | QFile::Text))
    25. {
    26. return;
    27. }
    28. QTextStream out(& file);
    29. out << filename << "\n";
    30.  
    31. file.close();
    32. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Verify data in a dialog

    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,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Verify data in a dialog

    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
    Qt Code:
    1. void CreateDialog::on_create_vehicle_btn_clicked()
    2. {
    3. // Check if name is entered.
    4. if(ui->vehicle_name_dialog_txtbox->text() == "")
    5. {
    6. NameDialog *name = new NameDialog(this);
    7. name->show();
    8.  
    9. connect(name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
    10. return;
    11. }
    12.  
    13. for(int i = 0; i < vehicle.size(); ++i)
    14. {
    15. if(ui->vehicle_name_dialog_txtbox->text() == vehicle[i])
    16. {
    17. NewNameDialog *new_name = new NewNameDialog(this);
    18. new_name->show();
    19.  
    20. connect(new_name, SIGNAL(name_Added(QString)), this, SLOT(update_name(QString)));
    21. return;
    22. }
    23. }
    24.  
    25. ...
    26. emit nameAdded(ui->vehicle_name_dialog_txtbox->text());
    27. ...
    28.  
    29. this->close();
    30. }
    To copy to clipboard, switch view to plain text mode 
    It is still overwriting the file though and now completely ignoring the new name.
    Last edited by admkrk; 7th July 2014 at 21:18.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Verify data in a dialog

    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    admkrk (7th July 2014)

  6. #5
    Join Date
    Apr 2014
    Posts
    59
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Verify data in a dialog

    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!

Similar Threads

  1. Replies: 3
    Last Post: 22nd April 2013, 23:44
  2. How to sign and verify through Openssl
    By mania in forum General Discussion
    Replies: 5
    Last Post: 11th March 2013, 04:54
  3. Passing data to dialog via dialog or a method
    By mtnbiker66 in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2012, 01:39
  4. Verify fields in QTableWidget are complete?
    By alitoh in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2011, 13:49
  5. Replies: 3
    Last Post: 1st September 2008, 23:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.