Fix custom type saving when only focus changes for user-entered type

At the moment when user types a custom type into the type combo box and
doesn't press enter, the entered type isn't used. This patch tries to fix
the problem by installing an event filter for the combo box and checking
when it loses focus - when it does, it performs type updates too.

On the way the patch also changes the signal used inside moveCurrentField()
from activated() to currentIndexChanged() to make it consistent with the
rest of the file.
This commit is contained in:
Jiří Techet
2017-09-19 11:29:00 +02:00
committed by Martin Kleusberg
parent 2f304e0957
commit 969d3e470a
2 changed files with 23 additions and 4 deletions

View File

@@ -119,6 +119,7 @@ void EditTableDialog::populateFields()
index = typeBox->count() - 1;
}
typeBox->setCurrentIndex(index);
typeBox->installEventFilter(this);
connect(typeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTypes()));
ui->treeWidget->setItemWidget(tbitem, kType, typeBox);
@@ -227,13 +228,13 @@ void EditTableDialog::checkInput()
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}
void EditTableDialog::updateTypes()
void EditTableDialog::updateTypes(QObject *object)
{
QComboBox* typeBox = qobject_cast<QComboBox*>(sender());
QComboBox* typeBox = qobject_cast<QComboBox*>(object);
if(typeBox)
{
QString type = typeBox->currentText();
QString column = sender()->property("column").toString();
QString column = typeBox->property("column").toString();
int index;
for(index=0; index < m_table.fields().size(); ++index)
@@ -249,6 +250,20 @@ void EditTableDialog::updateTypes()
}
}
void EditTableDialog::updateTypes()
{
updateTypes(sender());
}
bool EditTableDialog::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::FocusOut)
{
updateTypes(object);
}
return false;
}
void EditTableDialog::itemChanged(QTreeWidgetItem *item, int column)
{
int index = ui->treeWidget->indexOfTopLevelItem(item);
@@ -533,6 +548,7 @@ void EditTableDialog::addField()
}
ui->treeWidget->setItemWidget(tbitem, kType, typeBox);
typeBox->installEventFilter(this);
connect(typeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTypes()));
tbitem->setCheckState(kNotNull, Qt::Unchecked);
@@ -636,7 +652,8 @@ void EditTableDialog::moveCurrentField(bool down)
QComboBox* oldCombo = qobject_cast<QComboBox*>(ui->treeWidget->itemWidget(ui->treeWidget->topLevelItem(currentRow), kType));
QComboBox* newCombo = new QComboBox(ui->treeWidget);
newCombo->setProperty("column", oldCombo->property("column"));
connect(newCombo, SIGNAL(activated(int)), this, SLOT(updateTypes()));
newCombo->installEventFilter(this);
connect(newCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateTypes()));
newCombo->setEditable(true);
for(int i=0; i < oldCombo->count(); ++i)
newCombo->addItem(oldCombo->itemText(i));