qt4.键盘方向键

QT程序设计进阶-事件

Qt事件 

PS:出现焦点的话,方向键盘是没有进入事件的..

Qt程序是事件驱动的程序的每个动作都是由幕后某个事件所触发

Qt事件的类型很多常见的qt的事件如下:
键盘事件按键按下和松开.
鼠标事件鼠标移动,鼠标按键的按下和松开.
拖放事件用鼠标进行拖放.
滚轮事件鼠标滚轮滚动.
绘屏事件重绘屏幕的某些部分.
定时事件定时器到时.
焦点事件键盘焦点移动.
进入和离开事件鼠标移入widget之内,或是移出.
移动事件: widget的位置改变.
大小改变事件: widget的大小改变.
显示和隐藏事件: widget显示和隐藏.
窗口事件窗口是否为当前窗口.

还有一些非常见的qt事件,比如socket事件,剪贴板事件,字体改变,布局改变等等.

Qt 的事件和Qt中的signal不一样后者通常用来使用widget, 而前者用来实现 widget. 

比如一个按钮我们使用这个按钮的时候我们只关心他clicked()signal, 至于这个按钮如何接收处理鼠标事件,再发射这个信号,我们是不用关心的但是如果我们要重载一个按钮的时候,我们就要面对event比如我们可以改变它的行为,在鼠标按键按下的时候(mouse press event) 就触发clicked()signal而不是通常在释放的( mouse release event)时候.

事件起源:

基于事件如何被产生与分发,可以把事件分为三类:

1)Spontaneous 事件

2) Posted 事件

3)Sent  事件

1)Spontaneous 事件,由窗口系统产生,它们被放到系统队列中,通过事件循环逐个处理。

本类事件通常是window system把从系统得到的消息,比如鼠标按键,键盘按键等放入系统的消息队列中. Qt事件循环的时候读取这些事件,转化为QEvent,再依次处理.

2)Posted 事件,由Qt或是应用程序产生,它们被Qt组成队列,再通过事件循环处理。

调用QApplication::postEvent()来产生一个posted类型事件

例如:QWidget::update()函数

当需要重新绘制屏幕时,程序调用update()函数

其实现的原理是new出一个paintEvent,调用 QApplication::postEvent(),将其放入Qt的消息队列中,等待依次被处理

3)Sent  事件由Qt或是应用程序产生,但它们被直接发送到目标对象。

调用QApplication::sendEvent()函数来产生一个sent类型事件

sent 类型事件不会放入队列而是直接被派发和处理, QWidget::repaint()函数用的就是这种方式.

当我们在main()函数的末尾调用QApplication::exec(),程序进入了Qt的事件循环

事件循环如下面所示:

while (!exit_was_called)

{

  while(!posted_event_queue_is_empty)

       {

         process_next_posted_event();

       }

  while(!spontaneous_event_queue_is_empty)

      {

         process_next_spontaneous_event();

      }

  while(!posted_event_queue_is_empty)

      {

        process_next_posted_event();

      }

}

事件循环的处理流程:

1)先处理Qt事件队列中的posted事件,直至为空 

2)再处理系统消息队列中的spontaneous消息,直至为空 

3)在处理系统消息的时候会产生新的Qt posted事件,需要对其再次进行处理 

不通过事件循环

sendEvent的事件派发不通过事件循环。QApplication::sendEvent()是通过调用QApplication::notify(),直接进入了事件的派发和处理环节。

Notify

调用QApplication::sendEvent的时候消息会立即被处理,是同步的实际上QApplication::sendEvent()是通过调用QApplication::notify(), 直接进入了事件的派发和处理环节.所有的事件都最终通过 notify 派发到相应的对象中。

bool QApplication::notify ( QObject * receiver, QEvent * event ) 

它是通过调用receiver->event(event) 来实现的。

目标接受对象的event方法会自动接受notify传来的event事件

event() 会返回一个布尔值,来告诉调用者是否事件被acceptignore,

(true表示accept),从event()返回的布尔值却是用来与QApplication:notify()通讯的

event()函数的处理如下所示:

bool QWidget::event(QEvent *event)

    {

        switch (e->type()) {

        case QEvent::KeyPress:

            keyPressEvent((QKeyEvent *)event);

            if (!((QKeyEvent *)event)->isAccepted())

                return false;

            break;

        case QEvent::KeyRelease:

            keyReleaseEvent((QKeyEvent *)event);

            if (!((QKeyEvent *)event)->isAccepted())

                return false;

            break;

            ...

        }

        return true;

    }

Close事件有点不同,调用QCloseEvent:ignore()取消了关闭操作,而accept()告诉Qt继续执行正常的关闭操作。为了避免混乱,最好是在closeEvent()的新实现中明确地进行accept()ignore()的调用:、

 void MainWindow::closeEvent(QCloseEvent *event)

    {

        if (userReallyWantsToQuit()) {

            event->accept();

        } else {

            event->ignore();

        }

}

例子:keyPressEvent

在空白窗体页面,重载当前窗体类的keyPressEvent方法,实现按键事件的响应。

步骤一:

添加头文件<qevent.h>

form.cpp中填加void   Form1::keyPressEventQKeyEvent *k 

并实现根据不同的键值,执行不同的动作。

步骤二:

添加头文件<qevent.h>

form.h 中为窗体类form1添加 void  keyPressEventQKeyEvent *k )声明;

步骤三:

重新编译工程并运行测试。

void Form1::keyPressEvent( QKeyEvent *k )

{

    if(k->key() == Key_Left)

        {

                qDebug("Left\n");

               ....

        }

    else if(k->key() == Key_Right)

        {

                qDebug("Right\n");

               ...

        }

   else    QWidget::keyPressEvent(k);

}

在具备子控件的复杂窗体中,重载当前窗体类的keyPressEvent方法,实现按键事件的响应。

步骤一:

添加头文件<qevent.h>

form.cpp中填加void   Form1::keyPressEventQKeyEvent *k 

并实现根据不同的键值,执行不同的动作。

步骤二:

添加头文件<qevent.h>

form.h 中为窗体类form1添加 void  keyPressEventQKeyEvent *k )声明;

步骤三:

form.cpp中,消除子控件的焦点策略,使能方向及Tab按键功能。

步骤四:

重新编译工程并运行测试。

例如:

pushButton1 = new QPushButton( this, "pushButton1" );

pushButton1->setGeometry( QRect( 200, 150, 111, 41 ) );

pushButton1->setFocusPolicy(QWidget::NoFocus);

void QWidget::setFocusPolicy ( FocusPolicy ) 

设置这个窗口部件接收键盘焦点的方式。

“focusPolicy”属性保存的是窗口部件接收键盘焦点的策略。 

如果窗口部件通过tab来接收键盘焦点,这个策略就是QWidget::TabFocus

如果窗口部件通过点击来接收键盘焦点,这个策略就是QWidget::ClickFocus

如果窗口部件上述两种方式都使用,是QWidget::StrongFocus

如果它不接收焦点(QWidget的默认值),是QWidget::NoFocus

重载当前窗体类的event方法,实现针对性事件的处理与过滤效果。

步骤一:

form.cpp中填加bool  Form1::eventQEvent *event

并实现根据不同的键值,执行不同的动作。

步骤二:

form.h 中为窗体类form1添加 bool  eventQEvent *event)声明;

步骤三:

重新编译工程并运行测试。

bool Form1::event(QEvent * event)

{

    if (event->type() == QEvent::KeyPress)

    {

        QKeyEvent *keyEvent = (QKeyEvent *) event;

        if (keyEvent->key() == Key_A)

        {

            qDebug("--cut  the  Key_A--\n");

            return true;

        }

    }

    return QWidget::event(event);

}

实验:

1)用鼠标事件实现鼠标放在按钮上,按钮变大。

2)用按键事件实现方向右和方向左键控制2个窗口

3)用信号与槽机制实现鼠标点击nextback实现控制2个窗口

main.cpp:

[cpp]  view plain copy
  1. #include <qapplication.h>   
  2.   
  3. #include "form1.h"   
  4.   
  5. int main( int argc, char ** argv )  
  6.   
  7. {  
  8.   
  9.     QApplication a( argc, argv );  
  10.   
  11.     Form1 w;  
  12.   
  13.     w.show();  
  14.   
  15.     a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );  
  16.   
  17.     return a.exec();  
  18.   
  19. }  
[cpp]  view plain copy
  1. #include <qapplication.h>   
  2.   
  3. #include "form1.h"   
  4.   
  5. int main( int argc, char ** argv )  
  6.   
  7. {  
  8.   
  9.     QApplication a( argc, argv );  
  10.   
  11.     Form1 w;  
  12.   
  13.     w.show();  
  14.   
  15.     a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );  
  16.   
  17.     return a.exec();  
  18.   
  19. }  
[cpp]  view plain  copy
  1. #include <qapplication.h>  
  2.   
  3. #include "form1.h"  
  4.   
  5. int main( int argc, char ** argv )  
  6.   
  7. {  
  8.   
  9.     QApplication a( argc, argv );  
  10.   
  11.     Form1 w;  
  12.   
  13.     w.show();  
  14.   
  15.     a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );  
  16.   
  17.     return a.exec();  
  18.   
  19. }  


form1.cpp

[cpp]  view plain copy
  1. #include "form1.h"   
  2.   
  3. #include "form2.h"   
  4.   
  5. #include <qvariant.h>   
  6.   
  7. #include <qpushbutton.h>   
  8.   
  9. #include <qlayout.h>   
  10.   
  11. #include <qtooltip.h>   
  12.   
  13. #include <qwhatsthis.h>   
  14.   
  15. #include <qimage.h>   
  16.   
  17. #include <qpixmap.h>   
  18.   
  19. /* 
  20.  
  21.  *  Constructs a Form1 as a child of 'parent', with the 
  22.  
  23.  *  name 'name' and widget flags set to 'f'. 
  24.  
  25.  * 
  26.  
  27.  *  The dialog will by default be modeless, unless you set 'modal' to 
  28.  
  29.  *  TRUE to construct a modal dialog. 
  30.  
  31.  */  
  32.   
  33. Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )  
  34.   
  35.     : QDialog( parent, name, modal, fl )  
  36.   
  37. {  
  38.   
  39.     if ( !name )  
  40.   
  41. setName( "Form1" );  
  42.   
  43.     setMouseTracking (true);  
  44.   
  45.     pushButton1_3_2 = new QPushButton( this"pushButton1_3_2" );  
  46.   
  47.     pushButton1_3_2->setGeometry( QRect( 210, 80, 51, 41 ) );  
  48.   
  49.     pushButton1_3_2->setFocusPolicy(QWidget::NoFocus);  
  50.   
  51.     pushButton1_4_2 = new QPushButton( this"pushButton1_4_2" );  
  52.   
  53.     pushButton1_4_2->setGeometry( QRect( 140, 80, 51, 41 ) );  
  54.   
  55.     pushButton1_4_2->setFocusPolicy(QWidget::NoFocus);  
  56.   
  57.     pushButton1 = new QPushButton( this"pushButton1" );  
  58.   
  59.     pushButton1->setGeometry( QRect( 70, 20, 51, 41 ) );  
  60.   
  61.     pushButton1->setFocusPolicy(QWidget::NoFocus);  
  62.   
  63.     pushButton1_3 = new QPushButton( this"pushButton1_3" );  
  64.   
  65.     pushButton1_3->setGeometry( QRect( 210, 20, 51, 41 ) );  
  66.   
  67.     pushButton1_3->setFocusPolicy(QWidget::NoFocus);  
  68.   
  69.     pushButton1_5 = new QPushButton( this"pushButton1_5" );  
  70.   
  71.     pushButton1_5->setGeometry( QRect( 70, 80, 51, 41 ) );  
  72.   
  73.     pushButton1_5->setFocusPolicy(QWidget::NoFocus);  
  74.   
  75.     pushButton1_4 = new QPushButton( this"pushButton1_4" );  
  76.   
  77.     pushButton1_4->setGeometry( QRect( 140, 20, 51, 41 ) );  
  78.   
  79.     pushButton1_4->setFocusPolicy(QWidget::NoFocus);  
  80.   
  81.     pushButton1_2 = new QPushButton( this"pushButton1_2" );  
  82.   
  83.     pushButton1_2->setGeometry( QRect( 280, 20, 51, 41 ) );  
  84.   
  85.     pushButton1_2->setFocusPolicy(QWidget::NoFocus);  
  86.   
  87.     pushButton1_2_2 = new QPushButton( this"pushButton1_2_2" );  
  88.   
  89.     pushButton1_2_2->setGeometry( QRect( 280, 80, 51, 41 ) );  
  90.   
  91.     pushButton1_2_2->setFocusPolicy(QWidget::NoFocus);  
  92.   
  93.     pushButton9 = new QPushButton( this"pushButton9" );  
  94.   
  95.     pushButton9->setGeometry( QRect( 340, 160, 70, 30 ) );  
  96.   
  97.     pushButton9->setFocusPolicy(QWidget::NoFocus);  
  98.   
  99.     languageChange();  
  100.   
  101.     resize( QSize(434, 204).expandedTo(minimumSizeHint()) );  
  102.   
  103.     clearWState( WState_Polished );  
  104.   
  105.     // signals and slots connections   
  106.   
  107.     connect( pushButton9, SIGNAL( clicked() ), this, SLOT( next() ) );  
  108.   
  109. }  
  110.   
  111. /* 
  112.  
  113.  *  Destroys the object and frees any allocated resources 
  114.  
  115.  */  
  116.   
  117. Form1::~Form1()  
  118.   
  119. {  
  120.   
  121.     // no need to delete child widgets, Qt does it all for us   
  122.   
  123. }  
  124.   
  125. /* 
  126.  
  127.  *  Sets the strings of the subwidgets using the current 
  128.  
  129.  *  language. 
  130.  
  131.  */  
  132.   
  133. void Form1::languageChange()  
  134.   
  135. {  
  136.   
  137.     setCaption( tr( "Form1" ) );  
  138.   
  139.     pushButton1_3_2->setText( tr( "7" ) );  
  140.   
  141.     pushButton1_4_2->setText( tr( "6" ) );  
  142.   
  143.     pushButton1->setText( tr( "1" ) );  
  144.   
  145.     pushButton1_3->setText( tr( "3" ) );  
  146.   
  147.     pushButton1_5->setText( tr( "5" ) );  
  148.   
  149.     pushButton1_4->setText( tr( "2" ) );  
  150.   
  151.     pushButton1_2->setText( tr( "4" ) );  
  152.   
  153.     pushButton1_2_2->setText( tr( "8" ) );  
  154.   
  155.     pushButton9->setText( tr( "next" ) );  
  156.   
  157. }  
  158.   
  159. void Form1::next()  
  160.   
  161. {  
  162.   
  163.     Form2 a;  
  164.   
  165.     a.show();  
  166.   
  167.     close();  
  168.   
  169.     a.exec();  
  170.   
  171. }  
  172.   
  173. void Form1::press_next()  
  174.   
  175. {  
  176.   
  177.     Form2 a;  
  178.   
  179.     a.show();  
  180.   
  181.     close();  
  182.   
  183.     a.exec();  
  184.   
  185. }  
  186.   
  187. void Form1::keyPressEvent ( QKeyEvent * e )  
  188.   
  189. {  
  190.   
  191.     if(e->key()==Key_Right)  
  192.   
  193.         press_next();  
  194.   
  195.     else  
  196.   
  197.         QWidget::keyPressEvent (e);  
  198.   
  199. }  
  200.   
  201. void Form1:: mouseMoveEvent ( QMouseEvent * e )  
  202.   
  203. {  
  204.   
  205.     if(e->x() < 260 && e->x() > 210 && e->y() > 80 && e->y() < 120)  
  206.   
  207.        pushButton1_3_2-> resize(61,51);  
  208.   
  209.     else if(e->x() < 120 && e->x() > 70 && e->y() > 20 && e->y() < 60)  
  210.   
  211.        pushButton1->resize(61,51);  
  212.   
  213.     else if(e->x() < 260 && e->x() > 210 && e->y() > 20 && e->y() < 60)  
  214.   
  215.         pushButton1_3->resize(61,51);  
  216.   
  217.      else  
  218.   
  219.      {  
  220.   
  221.         pushButton1_3_2->resize(51,41);  
  222.   
  223.         pushButton1->resize(51,41);  
  224.   
  225.         pushButton1_3->resize(51,41);  
  226.   
  227.         QWidget::mouseMoveEvent(e);  
  228.   
  229.     }  
  230.   
  231. }  
[cpp]  view plain copy
  1. #include "form1.h"   
  2.   
  3. #include "form2.h"   
  4.   
  5. #include <qvariant.h>   
  6.   
  7. #include <qpushbutton.h>   
  8.   
  9. #include <qlayout.h>   
  10.   
  11. #include <qtooltip.h>   
  12.   
  13. #include <qwhatsthis.h>   
  14.   
  15. #include <qimage.h>   
  16.   
  17. #include <qpixmap.h>   
  18.   
  19. /* 
  20.  
  21.  *  Constructs a Form1 as a child of 'parent', with the 
  22.  
  23.  *  name 'name' and widget flags set to 'f'. 
  24.  
  25.  * 
  26.  
  27.  *  The dialog will by default be modeless, unless you set 'modal' to 
  28.  
  29.  *  TRUE to construct a modal dialog. 
  30.  
  31.  */  
  32.   
  33. Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )  
  34.   
  35.     : QDialog( parent, name, modal, fl )  
  36.   
  37. {  
  38.   
  39.     if ( !name )  
  40.   
  41. setName( "Form1" );  
  42.   
  43.     setMouseTracking (true);  
  44.   
  45.     pushButton1_3_2 = new QPushButton( this"pushButton1_3_2" );  
  46.   
  47.     pushButton1_3_2->setGeometry( QRect( 210, 80, 51, 41 ) );  
  48.   
  49.     pushButton1_3_2->setFocusPolicy(QWidget::NoFocus);  
  50.   
  51.     pushButton1_4_2 = new QPushButton( this"pushButton1_4_2" );  
  52.   
  53.     pushButton1_4_2->setGeometry( QRect( 140, 80, 51, 41 ) );  
  54.   
  55.     pushButton1_4_2->setFocusPolicy(QWidget::NoFocus);  
  56.   
  57.     pushButton1 = new QPushButton( this"pushButton1" );  
  58.   
  59.     pushButton1->setGeometry( QRect( 70, 20, 51, 41 ) );  
  60.   
  61.     pushButton1->setFocusPolicy(QWidget::NoFocus);  
  62.   
  63.     pushButton1_3 = new QPushButton( this"pushButton1_3" );  
  64.   
  65.     pushButton1_3->setGeometry( QRect( 210, 20, 51, 41 ) );  
  66.   
  67.     pushButton1_3->setFocusPolicy(QWidget::NoFocus);  
  68.   
  69.     pushButton1_5 = new QPushButton( this"pushButton1_5" );  
  70.   
  71.     pushButton1_5->setGeometry( QRect( 70, 80, 51, 41 ) );  
  72.   
  73.     pushButton1_5->setFocusPolicy(QWidget::NoFocus);  
  74.   
  75.     pushButton1_4 = new QPushButton( this"pushButton1_4" );  
  76.   
  77.     pushButton1_4->setGeometry( QRect( 140, 20, 51, 41 ) );  
  78.   
  79.     pushButton1_4->setFocusPolicy(QWidget::NoFocus);  
  80.   
  81.     pushButton1_2 = new QPushButton( this"pushButton1_2" );  
  82.   
  83.     pushButton1_2->setGeometry( QRect( 280, 20, 51, 41 ) );  
  84.   
  85.     pushButton1_2->setFocusPolicy(QWidget::NoFocus);  
  86.   
  87.     pushButton1_2_2 = new QPushButton( this"pushButton1_2_2" );  
  88.   
  89.     pushButton1_2_2->setGeometry( QRect( 280, 80, 51, 41 ) );  
  90.   
  91.     pushButton1_2_2->setFocusPolicy(QWidget::NoFocus);  
  92.   
  93.     pushButton9 = new QPushButton( this"pushButton9" );  
  94.   
  95.     pushButton9->setGeometry( QRect( 340, 160, 70, 30 ) );  
  96.   
  97.     pushButton9->setFocusPolicy(QWidget::NoFocus);  
  98.   
  99.     languageChange();  
  100.   
  101.     resize( QSize(434, 204).expandedTo(minimumSizeHint()) );  
  102.   
  103.     clearWState( WState_Polished );  
  104.   
  105.     // signals and slots connections   
  106.   
  107.     connect( pushButton9, SIGNAL( clicked() ), this, SLOT( next() ) );  
  108.   
  109. }  
  110.   
  111. /* 
  112.  
  113.  *  Destroys the object and frees any allocated resources 
  114.  
  115.  */  
  116.   
  117. Form1::~Form1()  
  118.   
  119. {  
  120.   
  121.     // no need to delete child widgets, Qt does it all for us   
  122.   
  123. }  
  124.   
  125. /* 
  126.  
  127.  *  Sets the strings of the subwidgets using the current 
  128.  
  129.  *  language. 
  130.  
  131.  */  
  132.   
  133. void Form1::languageChange()  
  134.   
  135. {  
  136.   
  137.     setCaption( tr( "Form1" ) );  
  138.   
  139.     pushButton1_3_2->setText( tr( "7" ) );  
  140.   
  141.     pushButton1_4_2->setText( tr( "6" ) );  
  142.   
  143.     pushButton1->setText( tr( "1" ) );  
  144.   
  145.     pushButton1_3->setText( tr( "3" ) );  
  146.   
  147.     pushButton1_5->setText( tr( "5" ) );  
  148.   
  149.     pushButton1_4->setText( tr( "2" ) );  
  150.   
  151.     pushButton1_2->setText( tr( "4" ) );  
  152.   
  153.     pushButton1_2_2->setText( tr( "8" ) );  
  154.   
  155.     pushButton9->setText( tr( "next" ) );  
  156.   
  157. }  
  158.   
  159. void Form1::next()  
  160.   
  161. {  
  162.   
  163.     Form2 a;  
  164.   
  165.     a.show();  
  166.   
  167.     close();  
  168.   
  169.     a.exec();  
  170.   
  171. }  
  172.   
  173. void Form1::press_next()  
  174.   
  175. {  
  176.   
  177.     Form2 a;  
  178.   
  179.     a.show();  
  180.   
  181.     close();  
  182.   
  183.     a.exec();  
  184.   
  185. }  
  186.   
  187. void Form1::keyPressEvent ( QKeyEvent * e )  
  188.   
  189. {  
  190.   
  191.     if(e->key()==Key_Right)  
  192.   
  193.         press_next();  
  194.   
  195.     else  
  196.   
  197.         QWidget::keyPressEvent (e);  
  198.   
  199. }  
  200.   
  201. void Form1:: mouseMoveEvent ( QMouseEvent * e )  
  202.   
  203. {  
  204.   
  205.     if(e->x() < 260 && e->x() > 210 && e->y() > 80 && e->y() < 120)  
  206.   
  207.        pushButton1_3_2-> resize(61,51);  
  208.   
  209.     else if(e->x() < 120 && e->x() > 70 && e->y() > 20 && e->y() < 60)  
  210.   
  211.        pushButton1->resize(61,51);  
  212.   
  213.     else if(e->x() < 260 && e->x() > 210 && e->y() > 20 && e->y() < 60)  
  214.   
  215.         pushButton1_3->resize(61,51);  
  216.   
  217.      else  
  218.   
  219.      {  
  220.   
  221.         pushButton1_3_2->resize(51,41);  
  222.   
  223.         pushButton1->resize(51,41);  
  224.   
  225.         pushButton1_3->resize(51,41);  
  226.   
  227.         QWidget::mouseMoveEvent(e);  
  228.   
  229.     }  
  230.   
  231. }  
[cpp]  view plain  copy
  1. #include "form1.h"  
  2.   
  3. #include "form2.h"  
  4.   
  5. #include <qvariant.h>  
  6.   
  7. #include <qpushbutton.h>  
  8.   
  9. #include <qlayout.h>  
  10.   
  11. #include <qtooltip.h>  
  12.   
  13. #include <qwhatsthis.h>  
  14.   
  15. #include <qimage.h>  
  16.   
  17. #include <qpixmap.h>  
  18.   
  19. /* 
  20.  
  21.  *  Constructs a Form1 as a child of 'parent', with the 
  22.  
  23.  *  name 'name' and widget flags set to 'f'. 
  24.  
  25.  * 
  26.  
  27.  *  The dialog will by default be modeless, unless you set 'modal' to 
  28.  
  29.  *  TRUE to construct a modal dialog. 
  30.  
  31.  */  
  32.   
  33. Form1::Form1( QWidget* parent, const char* name, bool modal, WFlags fl )  
  34.   
  35.     : QDialog( parent, name, modal, fl )  
  36.   
  37. {  
  38.   
  39.     if ( !name )  
  40.   
  41. setName( "Form1" );  
  42.   
  43.     setMouseTracking (true);  
  44.   
  45.     pushButton1_3_2 = new QPushButton( this"pushButton1_3_2" );  
  46.   
  47.     pushButton1_3_2->setGeometry( QRect( 210, 80, 51, 41 ) );  
  48.   
  49.     pushButton1_3_2->setFocusPolicy(QWidget::NoFocus);  
  50.   
  51.     pushButton1_4_2 = new QPushButton( this"pushButton1_4_2" );  
  52.   
  53.     pushButton1_4_2->setGeometry( QRect( 140, 80, 51, 41 ) );  
  54.   
  55.     pushButton1_4_2->setFocusPolicy(QWidget::NoFocus);  
  56.   
  57.     pushButton1 = new QPushButton( this"pushButton1" );  
  58.   
  59.     pushButton1->setGeometry( QRect( 70, 20, 51, 41 ) );  
  60.   
  61.     pushButton1->setFocusPolicy(QWidget::NoFocus);  
  62.   
  63.     pushButton1_3 = new QPushButton( this"pushButton1_3" );  
  64.   
  65.     pushButton1_3->setGeometry( QRect( 210, 20, 51, 41 ) );  
  66.   
  67.     pushButton1_3->setFocusPolicy(QWidget::NoFocus);  
  68.   
  69.     pushButton1_5 = new QPushButton( this"pushButton1_5" );  
  70.   
  71.     pushButton1_5->setGeometry( QRect( 70, 80, 51, 41 ) );  
  72.   
  73.     pushButton1_5->setFocusPolicy(QWidget::NoFocus);  
  74.   
  75.     pushButton1_4 = new QPushButton( this"pushButton1_4" );  
  76.   
  77.     pushButton1_4->setGeometry( QRect( 140, 20, 51, 41 ) );  
  78.   
  79.     pushButton1_4->setFocusPolicy(QWidget::NoFocus);  
  80.   
  81.     pushButton1_2 = new QPushButton( this"pushButton1_2" );  
  82.   
  83.     pushButton1_2->setGeometry( QRect( 280, 20, 51, 41 ) );  
  84.   
  85.     pushButton1_2->setFocusPolicy(QWidget::NoFocus);  
  86.   
  87.     pushButton1_2_2 = new QPushButton( this"pushButton1_2_2" );  
  88.   
  89.     pushButton1_2_2->setGeometry( QRect( 280, 80, 51, 41 ) );  
  90.   
  91.     pushButton1_2_2->setFocusPolicy(QWidget::NoFocus);  
  92.   
  93.     pushButton9 = new QPushButton( this"pushButton9" );  
  94.   
  95.     pushButton9->setGeometry( QRect( 340, 160, 70, 30 ) );  
  96.   
  97.     pushButton9->setFocusPolicy(QWidget::NoFocus);  
  98.   
  99.     languageChange();  
  100.   
  101.     resize( QSize(434, 204).expandedTo(minimumSizeHint()) );  
  102.   
  103.     clearWState( WState_Polished );  
  104.   
  105.     // signals and slots connections  
  106.   
  107.     connect( pushButton9, SIGNAL( clicked() ), this, SLOT( next() ) );  
  108.   
  109. }  
  110.   
  111. /* 
  112.  
  113.  *  Destroys the object and frees any allocated resources 
  114.  
  115.  */  
  116.   
  117. Form1::~Form1()  
  118.   
  119. {  
  120.   
  121.     // no need to delete child widgets, Qt does it all for us  
  122.   
  123. }  
  124.   
  125. /* 
  126.  
  127.  *  Sets the strings of the subwidgets using the current 
  128.  
  129.  *  language. 
  130.  
  131.  */  
  132.   
  133. void Form1::languageChange()  
  134.   
  135. {  
  136.   
  137.     setCaption( tr( "Form1" ) );  
  138.   
  139.     pushButton1_3_2->setText( tr( "7" ) );  
  140.   
  141.     pushButton1_4_2->setText( tr( "6" ) );  
  142.   
  143.     pushButton1->setText( tr( "1" ) );  
  144.   
  145.     pushButton1_3->setText( tr( "3" ) );  
  146.   
  147.     pushButton1_5->setText( tr( "5" ) );  
  148.   
  149.     pushButton1_4->setText( tr( "2" ) );  
  150.   
  151.     pushButton1_2->setText( tr( "4" ) );  
  152.   
  153.     pushButton1_2_2->setText( tr( "8" ) );  
  154.   
  155.     pushButton9->setText( tr( "next" ) );  
  156.   
  157. }  
  158.   
  159. void Form1::next()  
  160.   
  161. {  
  162.   
  163.     Form2 a;  
  164.   
  165.     a.show();  
  166.   
  167.     close();  
  168.   
  169.     a.exec();  
  170.   
  171. }  
  172.   
  173. void Form1::press_next()  
  174.   
  175. {  
  176.   
  177.     Form2 a;  
  178.   
  179.     a.show();  
  180.   
  181.     close();  
  182.   
  183.     a.exec();  
  184.   
  185. }  
  186.   
  187. void Form1::keyPressEvent ( QKeyEvent * e )  
  188.   
  189. {  
  190.   
  191.     if(e->key()==Key_Right)  
  192.   
  193.         press_next();  
  194.   
  195.     else  
  196.   
  197.         QWidget::keyPressEvent (e);  
  198.   
  199. }  
  200.   
  201. void Form1:: mouseMoveEvent ( QMouseEvent * e )  
  202.   
  203. {  
  204.   
  205.     if(e->x() < 260 && e->x() > 210 && e->y() > 80 && e->y() < 120)  
  206.   
  207.        pushButton1_3_2-> resize(61,51);  
  208.   
  209.     else if(e->x() < 120 && e->x() > 70 && e->y() > 20 && e->y() < 60)  
  210.   
  211.        pushButton1->resize(61,51);  
  212.   
  213.     else if(e->x() < 260 && e->x() > 210 && e->y() > 20 && e->y() < 60)  
  214.   
  215.         pushButton1_3->resize(61,51);  
  216.   
  217.      else  
  218.   
  219.      {  
  220.   
  221.         pushButton1_3_2->resize(51,41);  
  222.   
  223.         pushButton1->resize(51,41);  
  224.   
  225.         pushButton1_3->resize(51,41);  
  226.   
  227.         QWidget::mouseMoveEvent(e);  
  228.   
  229.     }  
  230.   
  231. }  


form2.cpp

[cpp]  view plain copy
  1. #include "form2.h"   
  2.   
  3. #include "form1.h"   
  4.   
  5. #include <qvariant.h>   
  6.   
  7. #include <qpushbutton.h>   
  8.   
  9. #include <qlabel.h>   
  10.   
  11. #include <qlayout.h>   
  12.   
  13. #include <qtooltip.h>   
  14.   
  15. #include <qwhatsthis.h>   
  16.   
  17. #include <qimage.h>   
  18.   
  19. #include <qpixmap.h>   
  20.   
  21. /* 
  22.  
  23.  *  Constructs a Form2 as a child of 'parent', with the 
  24.  
  25.  *  name 'name' and widget flags set to 'f'. 
  26.  
  27.  * 
  28.  
  29.  *  The dialog will by default be modeless, unless you set 'modal' to 
  30.  
  31.  *  TRUE to construct a modal dialog. 
  32.  
  33.  */  
  34.   
  35. Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl )  
  36.   
  37.     : QDialog( parent, name, modal, fl )  
  38.   
  39. {  
  40.   
  41.     if ( !name )  
  42.   
  43. setName( "Form2" );  
  44.   
  45.     pushButton19 = new QPushButton( this"pushButton19" );  
  46.   
  47.     pushButton19->setGeometry( QRect( 20, 160, 71, 31 ) );  
  48.   
  49.     textLabel1 = new QLabel( this"textLabel1" );  
  50.   
  51.     textLabel1->setGeometry( QRect( 130, 40, 171, 81 ) );  
  52.   
  53.     languageChange();  
  54.   
  55.     resize( QSize(435, 204).expandedTo(minimumSizeHint()) );  
  56.   
  57.     clearWState( WState_Polished );  
  58.   
  59.     // signals and slots connections   
  60.   
  61.     connect( pushButton19, SIGNAL( clicked() ), this, SLOT( back() ) );  
  62.   
  63. }  
  64.   
  65. /* 
  66.  
  67.  *  Destroys the object and frees any allocated resources 
  68.  
  69.  */  
  70.   
  71. Form2::~Form2()  
  72.   
  73. {  
  74.   
  75.     // no need to delete child widgets, Qt does it all for us   
  76.   
  77. }  
  78.   
  79. /* 
  80.  
  81.  *  Sets the strings of the subwidgets using the current 
  82.  
  83.  *  language. 
  84.  
  85.  */  
  86.   
  87. void Form2::languageChange()  
  88.   
  89. {  
  90.   
  91.     setCaption( tr( "Form2" ) );  
  92.   
  93.     pushButton19->setText( tr( "back" ) );  
  94.   
  95.     pushButton19->setFocusPolicy(QWidget::NoFocus);  
  96.   
  97.     textLabel1->setText( tr( "<h1>hello world</h1>" ) );  
  98.   
  99. }  
  100.   
  101. void Form2::back()  
  102.   
  103. {  
  104.   
  105.     Form1 a;  
  106.   
  107.     a.show();  
  108.   
  109.     close();  
  110.   
  111.     a.exec();  
  112.   
  113. }  
  114.   
  115. ;  
  116.   
  117. void Form2::press_back()  
  118.   
  119. {  
  120.   
  121.     Form1 a;  
  122.   
  123.     a.show();  
  124.   
  125.     close();  
  126.   
  127.     a.exec();  
  128.   
  129. }  
  130.   
  131. void Form2::keyPressEvent ( QKeyEvent * e )  
  132.   
  133. {  
  134.   
  135.     if(e->key()==Key_Left)  
  136.   
  137.          press_back();  
  138.   
  139.      else  
  140.   
  141.          QWidget::keyPressEvent (e);  
  142.   
  143. }  
[cpp]  view plain copy
  1. #include "form2.h"   
  2.   
  3. #include "form1.h"   
  4.   
  5. #include <qvariant.h>   
  6.   
  7. #include <qpushbutton.h>   
  8.   
  9. #include <qlabel.h>   
  10.   
  11. #include <qlayout.h>   
  12.   
  13. #include <qtooltip.h>   
  14.   
  15. #include <qwhatsthis.h>   
  16.   
  17. #include <qimage.h>   
  18.   
  19. #include <qpixmap.h>   
  20.   
  21. /* 
  22.  
  23.  *  Constructs a Form2 as a child of 'parent', with the 
  24.  
  25.  *  name 'name' and widget flags set to 'f'. 
  26.  
  27.  * 
  28.  
  29.  *  The dialog will by default be modeless, unless you set 'modal' to 
  30.  
  31.  *  TRUE to construct a modal dialog. 
  32.  
  33.  */  
  34.   
  35. Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl )  
  36.   
  37.     : QDialog( parent, name, modal, fl )  
  38.   
  39. {  
  40.   
  41.     if ( !name )  
  42.   
  43. setName( "Form2" );  
  44.   
  45.     pushButton19 = new QPushButton( this"pushButton19" );  
  46.   
  47.     pushButton19->setGeometry( QRect( 20, 160, 71, 31 ) );  
  48.   
  49.     textLabel1 = new QLabel( this"textLabel1" );  
  50.   
  51.     textLabel1->setGeometry( QRect( 130, 40, 171, 81 ) );  
  52.   
  53.     languageChange();  
  54.   
  55.     resize( QSize(435, 204).expandedTo(minimumSizeHint()) );  
  56.   
  57.     clearWState( WState_Polished );  
  58.   
  59.     // signals and slots connections   
  60.   
  61.     connect( pushButton19, SIGNAL( clicked() ), this, SLOT( back() ) );  
  62.   
  63. }  
  64.   
  65. /* 
  66.  
  67.  *  Destroys the object and frees any allocated resources 
  68.  
  69.  */  
  70.   
  71. Form2::~Form2()  
  72.   
  73. {  
  74.   
  75.     // no need to delete child widgets, Qt does it all for us   
  76.   
  77. }  
  78.   
  79. /* 
  80.  
  81.  *  Sets the strings of the subwidgets using the current 
  82.  
  83.  *  language. 
  84.  
  85.  */  
  86.   
  87. void Form2::languageChange()  
  88.   
  89. {  
  90.   
  91.     setCaption( tr( "Form2" ) );  
  92.   
  93.     pushButton19->setText( tr( "back" ) );  
  94.   
  95.     pushButton19->setFocusPolicy(QWidget::NoFocus);  
  96.   
  97.     textLabel1->setText( tr( "<h1>hello world</h1>" ) );  
  98.   
  99. }  
  100.   
  101. void Form2::back()  
  102.   
  103. {  
  104.   
  105.     Form1 a;  
  106.   
  107.     a.show();  
  108.   
  109.     close();  
  110.   
  111.     a.exec();  
  112.   
  113. }  
  114.   
  115. ;  
  116.   
  117. void Form2::press_back()  
  118.   
  119. {  
  120.   
  121.     Form1 a;  
  122.   
  123.     a.show();  
  124.   
  125.     close();  
  126.   
  127.     a.exec();  
  128.   
  129. }  
  130.   
  131. void Form2::keyPressEvent ( QKeyEvent * e )  
  132.   
  133. {  
  134.   
  135.     if(e->key()==Key_Left)  
  136.   
  137.          press_back();  
  138.   
  139.      else  
  140.   
  141.          QWidget::keyPressEvent (e);  
  142.   
  143. }  
[cpp]  view plain  copy
  1. #include "form2.h"  
  2.   
  3. #include "form1.h"  
  4.   
  5. #include <qvariant.h>  
  6.   
  7. #include <qpushbutton.h>  
  8.   
  9. #include <qlabel.h>  
  10.   
  11. #include <qlayout.h>  
  12.   
  13. #include <qtooltip.h>  
  14.   
  15. #include <qwhatsthis.h>  
  16.   
  17. #include <qimage.h>  
  18.   
  19. #include <qpixmap.h>  
  20.   
  21. /* 
  22.  
  23.  *  Constructs a Form2 as a child of 'parent', with the 
  24.  
  25.  *  name 'name' and widget flags set to 'f'. 
  26.  
  27.  * 
  28.  
  29.  *  The dialog will by default be modeless, unless you set 'modal' to 
  30.  
  31.  *  TRUE to construct a modal dialog. 
  32.  
  33.  */  
  34.   
  35. Form2::Form2( QWidget* parent, const char* name, bool modal, WFlags fl )  
  36.   
  37.     : QDialog( parent, name, modal, fl )  
  38.   
  39. {  
  40.   
  41.     if ( !name )  
  42.   
  43. setName( "Form2" );  
  44.   
  45.     pushButton19 = new QPushButton( this"pushButton19" );  
  46.   
  47.     pushButton19->setGeometry( QRect( 20, 160, 71, 31 ) );  
  48.   
  49.     textLabel1 = new QLabel( this"textLabel1" );  
  50.   
  51.     textLabel1->setGeometry( QRect( 130, 40, 171, 81 ) );  
  52.   
  53.     languageChange();  
  54.   
  55.     resize( QSize(435, 204).expandedTo(minimumSizeHint()) );  
  56.   
  57.     clearWState( WState_Polished );  
  58.   
  59.     // signals and slots connections  
  60.   
  61.     connect( pushButton19, SIGNAL( clicked() ), this, SLOT( back() ) );  
  62.   
  63. }  
  64.   
  65. /* 
  66.  
  67.  *  Destroys the object and frees any allocated resources 
  68.  
  69.  */  
  70.   
  71. Form2::~Form2()  
  72.   
  73. {  
  74.   
  75.     // no need to delete child widgets, Qt does it all for us  
  76.   
  77. }  
  78.   
  79. /* 
  80.  
  81.  *  Sets the strings of the subwidgets using the current 
  82.  
  83.  *  language. 
  84.  
  85.  */  
  86.   
  87. void Form2::languageChange()  
  88.   
  89. {  
  90.   
  91.     setCaption( tr( "Form2" ) );  
  92.   
  93.     pushButton19->setText( tr( "back" ) );  
  94.   
  95.     pushButton19->setFocusPolicy(QWidget::NoFocus);  
  96.   
  97.     textLabel1->setText( tr( "<h1>hello world</h1>" ) );  
  98.   
  99. }  
  100.   
  101. void Form2::back()  
  102.   
  103. {  
  104.   
  105.     Form1 a;  
  106.   
  107.     a.show();  
  108.   
  109.     close();  
  110.   
  111.     a.exec();  
  112.   
  113. }  
  114.   
  115. ;  
  116.   
  117. void Form2::press_back()  
  118.   
  119. {  
  120.   
  121.     Form1 a;  
  122.   
  123.     a.show();  
  124.   
  125.     close();  
  126.   
  127.     a.exec();  
  128.   
  129. }  
  130.   
  131. void Form2::keyPressEvent ( QKeyEvent * e )  
  132.   
  133. {  
  134.   
  135.     if(e->key()==Key_Left)  
  136.   
  137.          press_back();  
  138.   
  139.      else  
  140.   
  141.          QWidget::keyPressEvent (e);  
  142.   
  143. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值