[solved]Cannot convert from 'QEvent* to 'QKeyEvent*'
-
I'm trying to catch enter key. I use event filter. When I determine event as KeyPress and trying to conver QEvent* to QKeyEvent* it gives me an error messege 'invalid type conversion',
'static_cast' : cannot convert from 'QEvent *' to 'QKeyEvent *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
So i can't call key() function to determine what key was pressedelse if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
replyCklicked();
} -
Hi,
Can you share the complete implementation of your event filter ?
-
Hi, sure.
bool LogWindow::eventFilter(QObject* object, QEvent* event)
{
QString str = ui.textEdit->toPlainText();
if (event->type() == QEvent::FocusOut && str.isEmpty())
{
ui.textEdit->setTextColor(QColor(150, 150, 150));
ui.textEdit->insertPlainText("Enter your message...");
ui.textEdit->setTextColor(QColor(0, 0, 0));
}
else if (event->type() == QEvent::FocusIn)
ui.textEdit->clear();
else if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
replyCklicked();
}return false;
}
-
Missing
#include <QKeyEvent>
? -
Yes, of course. Thank you