PDA

View Full Version : Using QTableView & QSytledItemDelegate - Issue w/ closeAndCommitEditorLE() :core dump



FredB
18th January 2017, 19:23
Qt5
I am using an example straight out the book 'Advanced Qt Programming'

Have a cell in QTableView that when 'double click' generates
a QLineEdit widget where I override the QStyledItemDelegate::createEditor(),
setEditorData(), setModelData() functions. Everything is working fine. However,
the 'core dump' situations arises when the user types in something that is
not allowed ( i.e. certain text words are reserved ) - So I need to notify
the user with a QMessageBox() that this text is reserved. I am not sure
why this is causing the problem:

Here is the code excerpts to show what I am doing:
The core dump occurs in the function closeAndCommitEditorLE()
which is called when the user hits Return in the QLineEdit editor.

It only occurs after a call to issueDuplicateNameWarning() is called
which popups a QMessageBox() warning. I am using exec() to keep things
simple here. When the user clicks on OK the exec() call returns
and my next step is to call: emit closeEditor(pLineEdit). It is
here that the core dump occurs. I have no clue as to why. The stacktrace
is not very clear on what is occurring at this point. I can provide the stacktrace if
anyone would like to see it.

I have been looking at this and debugging for several days and still not sure
why what i am doing is causing such problems. Any feedback, comments or advice
would be much appreciated.

QWidget *DeviceObjectItemDelegate::createEditor(
QWidget *parent,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
if ( index.column() == S_DEV_OBJ_NAME )
{
QLineEdit *pEditor = new QLineEdit(parent);
/* If the user presses Return or Enter, we take this as confirmation
* of their edit.
*/
connect( pEditor, &QLineEdit::returnPressed,
this, &DeviceObjectItemDelegate::closeAndCommitEditorLE);
return pEditor;
}
else
{
return QStyledItemDelegate::createEditor( parent, option, index );
}
}

void DeviceObjectItemDelegate::setEditorData(
QWidget *editor,
const QModelIndex & index ) const
{
else if ( index.column() == S_DEV_OBJ_NAME )
{
QString deviceName = index.model()->data(index).toString();
QLineEdit *pLineEdit = qobject_cast<QLineEdit *>(editor);
pLineEdit->setText( deviceName);
}
else
{
QStyledItemDelegate::setEditorData( editor, index );
}
}

void DeviceObjectItemDelegate::setModelData(
QWidget *editor,
QAbstractItemModel *model,
const QModelIndex & index ) const
{
if ( index.column() == S_DEV_OBJ_NAME )
{
QLineEdit *pLineEdit = qobject_cast<QLineEdit *>(editor);
printf( "setModelData[%s]\n", pLineEdit->text().toStdString().c_str() );
model->setData( index, pLineEdit->text() );
}
else
{
QStyledItemDelegate::setModelData( editor, model, index );
}
}

void DeviceObjectItemDelegate:: closeAndCommitEditorLE()
{
QLineEdit *pLineEdit = qobject_cast<QLineEdit*>(sender() );
QString Name = pLineEdit->text();
m_pLineEditor = pLineEdit;

bool bIsDuplicate = m_pDuplicateDeviceNames->doesNameAlreadyExist(m_deviceType ,
m_tableType, Name );

if ( bIsDuplicate)
{
/* Name already exists - alert the user */
issueDuplicateNameWarning( Name );
/* ERROR OCCURS WHEN THE NEXT LINE IS EXECUTED */
emit closeEditor( pLineEdit);
emit clearOkToUpdateFlag();
return;
}

setFlag( true );

emit commitData( pLineEdit);
emit closeEditor( pLineEdit);
emit clearOkToUpdateFlag();
}

void DeviceObjectItemDelegate:: issueDuplicateNameWarning(
const QString & Name )
{
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle("Device Type Name - Duplicate" );
msgBox.setText("Duplicate Device Type Names are NOT allowed!");
msgBox.setInformativeText(tr("The Device Type Name [%1] is already in use.").arg(Name));
msgBox.exec();
}

d_stranz
18th January 2017, 22:25
I don't see anything significantly different from your code and Summerfield's, except for your failures to check if the QLineEdit pointer returned from qobject_cast is non-null. Sure, it *shouldn't* ever be null but what happens if it is?

My guess is that something in the code that you haven't posted is causing some stack corruption, and the error just happens to manifest itself when you get to that part of the code.

Please use CODE tags when posting source code - see my signature block for how to do that.