KeyEvent on StackedWidget



  • 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??
    }


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Your static_cast is wrong: ev is a pointer.

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



  • 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;
        }
    }
    

  • Lifetime Qt Champion

    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 :)


Log in to reply
 

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