Don't add any entry whose name doesn't start with a letter

In the Addressbook example, any entry whose name doesn't start
with a letter will not appear in any of the categories,
but it will still exist. These entries would make the
"There are currently no contacts in your address book" page
disappear even when no entries could be found in the categories.

Check if a new entry has a name that doesn't start with a letter,
and if so, don't add it as a contact. Also, notify user that names
must start with a letter when this happens.

Fixes: QTBUG-124254
Pick-to: 6.7 6.6 6.5 6.2
Change-Id: I7f25711785ec7a82852a0f37d9f096cc3af41576
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Chris Lerner 2024-04-05 13:01:46 +02:00
parent b79cb3dd65
commit c3fa43513e

View File

@ -34,7 +34,10 @@ void AddressWidget::showAddEntryDialog()
//! [3] //! [3]
void AddressWidget::addEntry(const QString &name, const QString &address) void AddressWidget::addEntry(const QString &name, const QString &address)
{ {
if (!table->getContacts().contains({ name, address })) { if (!name.front().isLetter()) {
QMessageBox::information(this, tr("Invalid name"),
tr("The name must start with a letter."));
} else if (!table->getContacts().contains({ name, address })) {
table->insertRows(0, 1, QModelIndex()); table->insertRows(0, 1, QModelIndex());
QModelIndex index = table->index(0, 0, QModelIndex()); QModelIndex index = table->index(0, 0, QModelIndex());