Results 1 to 2 of 2

Thread: validaton on file name

  1. #1
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default validaton on file name

    I won't to enforce a QMessage box warning stating the user that the character length is too long for a particular name. I want the character name to be > than 10 characters.

    Here is my code so far.
    Qt Code:
    1. bool MainWindow::saveFile(const QString &fileName)
    2. {
    3. QFile file(fileName);
    4. [B] QString ext = fileName.completeSuffix();
    5. if(ext.completeSuffix() > 10)
    6. {
    7. QMessageBox::warning(this, tr("BAD FILE NAME"));
    8. }
    9. else[/B]
    10. if (!file.open(QFile::WriteOnly | QFile::Text)) {
    11. QMessageBox::warning(this, tr("Application"),
    12. tr("Cannot write file %1:\n%2.")
    13. .arg(fileName)
    14. .arg(file.errorString()));
    15. return false;
    16. }
    17.  
    18. QTextStream out(&file);
    19. #ifndef QT_NO_CURSOR
    20. QApplication::setOverrideCursor(Qt::WaitCursor);
    21. #endif
    22. out << textEdit->toPlainText();
    23. #ifndef QT_NO_CURSOR
    24. QApplication::restoreOverrideCursor();
    25. #endif
    26.  
    27. setCurrentFile(fileName);
    28. statusBar()->showMessage(tr("File saved"), 2000);
    29. return true;
    30. }
    To copy to clipboard, switch view to plain text mode 
    For some reason I can't figure out a way to enforce this validation as I'm fairly new to Qt. Does anyone have any suggestions for this? Thank you so much.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: validaton on file name

    I guess you have to start a little bit earlier. I guess you querying the name via a QLineEdit and then the user press a button. only set the button enabled if the line edit contains more the 10 characters. do that with a custom slot which which the textChanged signal of the line edit is connected to.

    otherwise just add an "return false;" after your message box.

Similar Threads

  1. Replies: 0
    Last Post: 12th April 2010, 07:05
  2. Replies: 3
    Last Post: 28th March 2009, 15:37
  3. Replies: 0
    Last Post: 6th March 2009, 08:19
  4. Replies: 2
    Last Post: 2nd April 2008, 17:28
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

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.