PDA

View Full Version : KeyEvent on StackedWidget



Benj
14th April 2017, 23:47
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);
this-> tcpSocket->closeSocket();

}

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

high_flyer
14th April 2017, 23:56
you forgot the pointer '*':

QKeyEvent* key = static_cast<QKeyEvent>(ev);
Why are you using static_cast??
I think what you are after is dynamic_cast<>

d_stranz
15th April 2017, 04:14
you forgot the pointer '*':

So did you :)


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

Benj
15th April 2017, 10:47
I have the pointers set now, such as bottom in the cut out of my code. It works finaly, Thank You!!!


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

high_flyer
16th April 2017, 22:02
So did you

Hehe, true! :-)

Added after 11 minutes:


I have the pointers set now, such as bottom in the cut out of my code. It works finaly, Thank You!!!
Our help here was giving you a fish, but it seems to me you need to learn how to fish, do you understand what your error was?
Its seems to me you do not understand what pointers are, and what static_cast vs dynamic_cast do.
I urge you to look C++ basics first before you do any further work with C++ otherwise you will get in to a lot of frustration.