Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. KeyEvent on StackedWidget
Forum Updated to NodeBB v4.3 + New Features

KeyEvent on StackedWidget

Scheduled Pinned Locked Moved Solved Game Development
4 Posts 2 Posters 1.5k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Benj
    wrote on last edited by
    #1

    Hey, at first i must apologize for my poor english. I am new in Qt and seek for an solution of the follow issue. First my code:

    bool MainWindow::eventFilter(QObject *obj, QEvent *ev){
    if((obj == ui->stackedWidget) && (ui->stackedWidget->currentIndex() == 1)){
    if(ev->type() == QEvent::KeyPress)
    {
    QKeyEvent key = static_cast<QKeyEvent>(ev);
    if(key.key() == Qt::Key_Control){
    ui->stackedWidget->setCurrentIndex(0);
    }

        }
        else{
            QPoint position = this->mapToGlobal(this->pos());
            int x = position.x(); int y=position.y();
            this->cursor().setPos((x/2)+175, (y/2)+175);
        }
    
        return true;
    }
    

    after my attempt to check wether control key was pressed, about a key event in this function, i received folllow error message:

    Error: no matching function for call to 'QKeyEvent::QKeyEvent(QEvent*&)'
    QKeyEvent key = static_cast<QKeyEvent>(ev);

    somebody has an idea??
    }

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

      Hi and welcome to devnet,

      Your static_cast is wrong: ev is a pointer.

      QKeyEvent *key = static_cast<QKeyEvent *>(ev);

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

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Benj
        wrote on last edited by
        #3

        I have the pointers set, such as bottom in the cut out of my code. It works finaly, Thank You!!!

        //your code here
        bool MainWindow::eventFilter(QObject *obj, QEvent *ev){
            if((obj == ui->stackedWidget) && (ui->stackedWidget->currentIndex() == 1)){
                if(ev->type() == QEvent::KeyPress)
                {
                    QKeyEvent *key = dynamic_cast<QKeyEvent*>(ev);
                    if(key->key() == Qt::Key_Control){
        
                        ui->stackedWidget->setCurrentIndex(0);
                        ui->stackedWidget->setCursor(Qt::ArrowCursor);
                        this->socket->closeTcp();
                        return true;
                    }
        
                }
                else{
                    this->position = this->mapToGlobal(this->pos());
                    int x = position.x(); int y=position.y();
                    this->cursor().setPos((x/2)+175, (y/2)+175);
                    return true;
                }
        
                return false;
            }
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're welcome !

          Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

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

          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