qt 键盘事件......

来源:互联网 发布:上瘾网络剧背景音乐 编辑:程序博客网 时间:2024/06/06 15:01

程序功能实现两个窗口,然后按a实现两个窗口乒乓.......

/*kb.h*/

#ifndef KB_H

#define KB_H#include<QPushButton>#include<QEvent>#include<QtGui>class Kb:public QDialog{ Q_OBJECT public: Kb(QWidget *parent=0); void keyPressEvent(QKeyEvent *k); QObject *obj; QPushButton *button; Kb *next;//这个是我想出来的,嘿嘿........};

#endif

/*kb.cpp*/

#include"kb.h"

 

Kb::Kb(QWidget *parent):QDialog(parent)

{

    //button=new QPushButton;

  //  button->setText("test");

}

 

void Kb::keyPressEvent(QKeyEvent *k)

{

    if(k->key() == Qt::Key_A) //判断是否是A键按下

    {

        this->next->show();

        this->hide();

        this->installEventFilter(this->next); /*安装事件过滤器,我的理解就是把键盘事件传递给谁........*/

    }

}

/*main.cpp*/

#include<QApplication>

#include<QPushButton>

#include<QEvent>

#include"kb.h"

int main(int argc,char *argv[])

{

    QApplication app(argc,argv);

    Kb kb,kb1;

    kb.setWindowTitle("MAIN WINDOW");

    kb1.setWindowTitle("Child WINDOW");

    kb.next=&kb1;

    kb1.next=&kb;

    kb1.show();

    return app.exec();

}