Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]Cannot convert from 'QEvent* to 'QKeyEvent*'

[solved]Cannot convert from 'QEvent* to 'QKeyEvent*'

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 12.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AntonZelenin
    wrote on last edited by AntonZelenin
    #1

    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();
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you share the complete implementation of your event filter ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you share the complete implementation of your event filter ?

        A Offline
        A Offline
        AntonZelenin
        wrote on last edited by AntonZelenin
        #3

        @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;
        

        }

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Missing #include <QKeyEvent> ?

          A 1 Reply Last reply
          5
          • Chris KawaC Chris Kawa

            Missing #include <QKeyEvent> ?

            A Offline
            A Offline
            AntonZelenin
            wrote on last edited by
            #5

            @Chris-Kawa

            Yes, of course. Thank you

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved