[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 pressed

    else if (event->type() == QEvent::KeyPress)
    {
    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
    replyCklicked();
    }


  • Lifetime Qt Champion

    Hi,

    Can you share the complete implementation of your event filter ?



  • @SGaist

    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;
    

    }


  • Moderators

    Missing #include <QKeyEvent> ?



  • @Chris-Kawa

    Yes, of course. Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.