0

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;  
    }
}
Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
Suresh
  • 745
  • 4
  • 11
  • 25

2 Answers2

0

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;    
    }
}
thuga
  • 12,601
  • 42
  • 52
  • -1: What about the `button2` and what about dynamic connections? – RedX Jan 14 '14 at 15:57
  • You can use hasFocus() and check whether the button focus is in button1 or 2 and then call that button by click() – Suresh Jan 15 '14 at 03:26
  • @RedX What about dynamic connections? OP didn't ask anything related to that. And `button2` works the same way. I demonstrated how to click a button programmatically. – thuga Jan 15 '14 at 08:08
0

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

Marek R
  • 32,568
  • 6
  • 55
  • 140