How to merge KeyReleaseEvent with Button

167 views Asked by At

How to merge KeyReleaseEvent and QPushButton using signal. I mean whenever user will press enter key button should call some function using SLOT. So what i have to use in the signal?

void mywindow::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_Enter:
            connect(button1, SIGNAL(clicked()), this, SLOT(fileNew()));
            connect(button2, SIGNAL(clicked()), this, SLOT(file()));
        break;  
    }
}
2

There are 2 answers

3
thuga On BEST ANSWER

If I understand your question correctly, you want to click some button when pressing the enter key. You can just call the QAbstractButton::click() function to perform a click.

connect(button1,SIGNAL(clicked()),this,SLOT(fileNew()));
connect(button2,SIGNAL(clicked()),this,SLOT(file())); //do this in your constructor, or somewhere else.. just make sure you only do this once

 

void mywindow::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_Enter:
            button1->click();    
        break;    
    }
}
0
Marek R On

There is shortcut property to handle such case.
I recommend to use QAction with shortcut value. There is lost of bonus functionality.