I am unfamiliar with customEvent. Since I already have an event filter for my base Table class, can I use it to receive the custom events?
I'm posting custom events from my setModelData() function but they are never received by my customEvent() handler. Clearly I'm misunderstanding how I need to connect these together. Any suggestions?
These are my class definitions:
class TableEvent
: public QEvent{
public:
TableEvent
( Type type,
const QString
& sOrigValue
) : QEvent( type
) {
m_sOrigValue = sOrigValue;
}
public:
};
class TableKeyHandler
: public QObject{
Q_OBJECT
public:
TableKeyHandler( Table* parent = 0 ) { m_pParent = parent; }
protected:
void customEvent( TableEvent* pEvent );
private:
Table* m_pParent;
};
class TableEvent : public QEvent
{
public:
TableEvent( Type type, const QString& sOrigValue ) : QEvent( type )
{
m_sOrigValue = sOrigValue;
}
public:
QString m_sOrigValue;
};
class TableKeyHandler : public QObject
{
Q_OBJECT
public:
TableKeyHandler( Table* parent = 0 ) { m_pParent = parent; }
protected:
bool eventFilter( QObject* obj, QEvent* pEvent );
void customEvent( TableEvent* pEvent );
private:
Table* m_pParent;
};
To copy to clipboard, switch view to plain text mode
And this is how I'm attempting to process events?
bool TableKeyHandler
::eventFilter( QObject* obj,
QEvent* pEvent
) {
if ( pEvent
->type
() == QEvent::KeyPress ) {
QKeyEvent* pKeyEvent
= static_cast<QKeyEvent
*>
( pEvent
);
int nKey = pKeyEvent->key();
. . .
}
}
void TableCellKeyHandler::customEvent( TableEvent* pEvent )
{
TableEvent* pTableEvent = static_cast<TableEvent*>( pEvent );
if ( pEvent->type() == EVENT_NAME_CHANGE )
{
// do validation of Name here
pEvent->accept();
}
else
pEvent->ignore();
}
bool TableKeyHandler::eventFilter( QObject* obj, QEvent* pEvent )
{
if ( pEvent->type() == QEvent::KeyPress )
{
QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>( pEvent );
int nKey = pKeyEvent->key();
. . .
}
}
void TableCellKeyHandler::customEvent( TableEvent* pEvent )
{
TableEvent* pTableEvent = static_cast<TableEvent*>( pEvent );
if ( pEvent->type() == EVENT_NAME_CHANGE )
{
// do validation of Name here
pEvent->accept();
}
else
pEvent->ignore();
}
To copy to clipboard, switch view to plain text mode
And this is how I'm attempting to post a custom event:
const QModelIndex& index ) const
{
if ( index.column() == COL_Name )
{
QLineEdit* lineEdit
= qobject_cast<QLineEdit
*>
( editor
);
// Validate the Name string
if ( (lineEdit != NULL) || !lineEdit->text().isEmpty() )
{
QString sOrigName
= index.
data( Qt
::EditRole ).
toString();
TableEvent* pEvent = new TableEvent( EVENT_NAME_CHANGE, sOrigName );
QObject* pObj
= qobject_cast<QObject
*>
(m_parent
);
static_cast<DMXTable*>( m_parent )->m_parent->m_pApp->postEvent( pObj, pEvent );
}
}
. . .
}
void DMXTableCellDelegate::setModelData( QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index ) const
{
if ( index.column() == COL_Name )
{
QLineEdit* lineEdit = qobject_cast<QLineEdit*>( editor );
// Validate the Name string
if ( (lineEdit != NULL) || !lineEdit->text().isEmpty() )
{
QString sOrigName = index.data( Qt::EditRole ).toString();
TableEvent* pEvent = new TableEvent( EVENT_NAME_CHANGE, sOrigName );
QObject* pObj = qobject_cast<QObject*>(m_parent);
static_cast<DMXTable*>( m_parent )->m_parent->m_pApp->postEvent( pObj, pEvent );
}
}
. . .
}
To copy to clipboard, switch view to plain text mode
Bookmarks