I created a simple QListView
added a QStringListModel
that allow items to be added and their text edited. But I don't want to allow empty fields to be added and I partially achieved this by using the dataChanged
signal from the model which is emitted if the list item loses focus without text inserted or the user presses the Enter
key without adding text.
However if you press the Esc
key, the field remains empty and no dataChanged
signal is emitted. How can I get notified if the field was left empty without overloading the QListView
class which would be tedious(I used the designer to create the form)?
Is there another signal that is emitted or method I can use to achieve that?
Thanks!
Use event filters in your main GUI class:
void GUI::GUI()
{
ui->mListView->installEventFilter(this);
}
bool GUI::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->mListView && event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->key() == Qt::Key_Escape)
// special Esc handling here
}
else
return false;
}
It's simple to use your custom widgets in Qt Designer. Right click on the QListView and choose Promote to ... there add a new class and apply it to the widget.
http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html