Search code examples
qteventsqt-creatorsignalssignals-slots

Need help on Event handling to signalling in QTcreator


So basically what I am trying to do is the following: I want to create a directional arrows pad on the screen. When the user presses the up or 8 key, the UI should react as if I clicked the up button. I have googled and searched all over, but as I just started using QTCreator (and C++), I am very inexperienced and any help would be appreciated.

So far I have

class GamePadWidget : public QWidget
    {
    public:
        GamePadWidget(QWidget *parent = 0);

    protected:
        virtual void keyPressEvent(QKeyEvent *event);

    };
GamePadWidget::GamePadWidget(QWidget *parent)
    : QWidget(parent)
{

    int buttonWidth = 75;
    int buttonHeight = 75;

    QPushButton *down = new QPushButton(("Y-"), this);;
    down->setGeometry(100, 200, 100, 100);
    QIcon downicon;
    downicon.addFile(QString::fromUtf8("C:/arrows/Aiga_downarrow.png"), QSize(),QIcon::Normal, QIcon::Off);
    down->setIcon(downicon);
    down->setIconSize(QSize(buttonWidth,buttonHeight));
    down->setFocusPolicy(Qt::NoFocus);

    QPushButton *up = new QPushButton(("Y+"), this);;
    up->setGeometry(100, 50, 100, 100);
    QIcon upicon;
    upicon.addFile(QString::fromUtf8("C:/arrows/Aiga_uparrow.png"), QSize(),QIcon::Normal, QIcon::Off);
    up->setIcon(upicon);
    up->setIconSize(QSize(buttonWidth,buttonHeight));
    up->setFocusPolicy(Qt::NoFocus);

}
void GamePadWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_8 || event->key() == Qt::Key_Up ) {
        printf("key event in board");

   }

   else if (event->key() == Qt::Key_9 || event->key() == Qt::Key_Down ) {
         qApp->quit();

    }
}

int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     GamePadWidget widget;
     widget.show();
     return app.exec();
 }

with my current code, if I press down or 2, the app exits as expected, yet here is part in which I am stuck at.

I want the same functionality as if I pressed the down (or up key), the pushbutton should light up briefly then shoot off a signal to who knows where

I realize it should have something to do with connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

but cannot quite wrap my mind / find it.

Thank you for your time.


Solution

  • You can call a slot on an object as if it was a normal method (which it is as far as C++ is concerned). Obv you'll need to make your pushButton a member though, so you have access to it outside of the constructor.

    Then yes, just connect the button's clicked() signal to the app's quit() slot. The code below should work for you (not tested though):

    GamePadWidget::GamePadWidget(QWidget *parent)
      : QWidget(parent)
    {
      ...
      mDownButton = new QPushButton(("Y-"), this);;
      ...
      connect(mDownButton, SIGNAL(clicked()), qApp, SLOT(quit()));
    }
    
    void GamePadWidget::keyPressEvent(QKeyEvent *event)
    {
      if (event->key() == Qt::Key_Down ) {
        qDebug() << "Down key pressed";
        mDownButton.click();
      }      
    }