PDA

View Full Version : validaton on file name



fortyhideout12
26th April 2010, 20:27
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.
bool MainWindow::saveFile(const QString &fileName)
{
QFile file(fileName);
QString ext = fileName.completeSuffix();
if(ext.completeSuffix() > 10)
{
QMessageBox::warning(this, tr("BAD FILE NAME"));
}
else
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("Application"),
tr("Cannot write file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}

QTextStream out(&file);
#ifndef QT_NO_CURSOR
QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
out << textEdit->toPlainText();
#ifndef QT_NO_CURSOR
QApplication::restoreOverrideCursor();
#endif

setCurrentFile(fileName);
statusBar()->showMessage(tr("File saved"), 2000);
return true;
}

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.

Lykurg
26th April 2010, 20:52
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.