当前位置:   article > 正文

QT中的事件_qt事件

qt事件

目录

1、QT事件

1.1、事件介绍

1.2、事件的处理

2、键盘事件

2.1、keyPressEvent

2.1.1、判断某个键按下

 2.1.2、组合键操作

3、鼠标事件

3.1、鼠标单击事件

3.2、鼠标释放事件

3.3、鼠标双击事件

3.4、鼠标移动事件

3.5、滚轮事件

4、事件过滤器

4.1、事件过滤器使用

5、定时器

5.1、QTimerEvent定时器实现

5.2、QTimer定时器实现

6、随机数种子

7、拖拽事件

拓(设置右键菜单栏):


1、QT事件

1.1、事件介绍

        事件是对各种应用程序需要知道的由应用程序内部或者外部产生的事情或者动作的通称。在Qt中使用一个对象来表示一个事件,它继承自QEvent类。

常见事件:鼠标事件、键盘事件、定时事件、上下文菜单事件、关闭事件、拖放事件、绘制事件。

1.2、事件的处理

重载相关的Event函数

重新实现事件的paintEvent(),mousePressEvent()等事件处理函数。这是最常用也的一种方法,不过它只能用来处理特定部件的特定事件。例如实现拖放操作,就是用的这种方法。

安装事件过滤器

在对象上安装事件过滤器。使用事件过滤器可以在一个界面类中同时处理不同子部件的不同事件。

2、键盘事件

QKeyEvent

QKeyEvent类用来描述一个键盘事件。当键盘按键被按下或者被释放时,键盘事件便会被发送给拥有键盘输入焦点的部件。

在Qt助手中,查找一下Qt::Key。

例:Qt::Key_Escape表示空格,Qt::Key_Left←键等等

2.1、keyPressEvent

        这个是需要包含头文件才能用,下面是从头文件中拿出来的

  1. protected:    
  2. bool event(QEvent *event) Q_DECL_OVERRIDE;    
  3. virtual void mousePressEvent(QMouseEvent *event);//鼠标按下    
  4. virtual void mouseReleaseEvent(QMouseEvent *event);//鼠标释放    
  5. virtual void mouseDoubleClickEvent(QMouseEvent *event);//鼠标双击    
  6. virtual void mouseMoveEvent(QMouseEvent *event);//鼠标移动
  7. #ifndef QT_NO_WHEELEVENT    
  8. virtual void wheelEvent(QWheelEvent *event); //滚轮
  9. #endif    
  10. virtual void keyPressEvent(QKeyEvent *event);//按键按下    
  11. virtual void keyReleaseEvent(QKeyEvent *event);//按键释放    
  12. virtual void enterEvent(QEvent *event);//进入    
  13. virtual void leaveEvent(QEvent *event);//离开    
  14. virtual void paintEvent(QPaintEvent *event);//绘制    
  15. virtual void moveEvent(QMoveEvent *event);//离开

         这里父类是怎么定义的虚函数,和声明的我们就怎么实现它

  1. //自己的类中去定义,然后实现
  2. protected:
  3. virtual void keyPressEvent(QKeyEvent *event);//按键按下

2.1.1、判断某个键按下

  1. void Widget::keyPressEvent(QKeyEvent *event){    
  2. if(event->key()==Qt::Key_X)   {        
  3. qDebug()<<"X按下";  
  4. }
  5. }

 2.1.2、组合键操作

        在Qt助手中搜索Qt::KeyboardModifier,如下 

  1. void Widget::keyPressEvent(QKeyEvent *event){    
  2. if(event->modifiers()==Qt::ControlModifier)//判断是不是ctrl按下
  3. {        
  4. if(event->key()==Qt::Key_X)//接着判断是不是x按下
  5. {            
  6. qDebug()<<"ctrl+X";      
  7. }  
  8. }else if(event->key()==Qt::Key_X)  
  9. {        qDebug()<<"X按下";  
  10. }
  11. }

3、鼠标事件

        QMouseEvent类用来表示一个鼠标事件,当在窗口部件中按下鼠标或者移动鼠标指针时,都会产生鼠标事件。利用QMouseEvent类可以获知鼠标是哪个键按下了,还有鼠标指针的当前位置等信息。通常是重定义部件的鼠标事件处理函数来进行一些自定义的操作。  

        QWheelEvent类用来表示鼠标滚轮事件,在这个类中主要是获取滚轮移动的方向和距离。在滚轮事件处理函数中,使用QWheelEvent类的delta()函数获取了滚轮移动的距离,每当滚轮旋转一下,默认的是15度,当滚轮向远离使用者的方向旋转时,返回正值;当向着靠近使用者的方向旋转时,返回负值。这样便可以利用这个函数的返回值来判断滚轮的移动方向。

3.1、鼠标单击事件

  1. Qt::LeftButton 左
  2. Qt::RightButton 右
  3. Qt::MidButton 中
  4. void Widget::mousePressEvent(QMouseEvent *event){    
  5. if(event->button()==Qt::LeftButton)   {        
  6. qDebug()<<"左键按下";  
  7. }
  8. }

3.2、鼠标释放事件

  1. void Widget::mouseReleaseEvent(QMouseEvent *event){    
  2. if(event->button()==Qt::LeftButton)   {        
  3. qDebug()<<"左键释放";  
  4. }
  5. }

3.3、鼠标双击事件

  1. void Widget::mouseDoubleClickEvent(QMouseEvent *event){    
  2. if(event->button()==Qt::LeftButton)   {        
  3. qDebug()<<"左键双击";  
  4. }
  5. }

3.4、鼠标移动事件

        setMouseTracking(true); 这个默认是为false,需要设置为true,这样才能实时获取鼠标位置,要不然只有在按下的时候才能获取到

  1. void Widget::mouseMoveEvent(QMouseEvent *event)
  2. {    
  3. qDebug()<<event->x()<<event->y();    
  4. qDebug()<<event->pos();//相对于窗口的坐标    
  5. qDebug()<<event->globalPos();//相对于屏幕的坐标
  6. }

3.5、滚轮事件

  1. void Widget::wheelEvent(QWheelEvent *event){    
  2. static int x=0;    
  3. x+=event->delta();//还是±120    
  4. if(event->delta()>0)  
  5. {        
  6. qDebug()<<"滚轮往前"<<x;  
  7. }else{        
  8. qDebug()<<"滚轮往后"<<x;  
  9. }
  10. }

4、事件过滤器

         有事件产生,在这个控件上只处理什么事件

         例如:在某个对话框输入任意信息但是是不会被输入到里面,会被获取

4.1、事件过滤器使用

  1. //1、安装过滤器
  2. ui->textEdit->installEventFilter(this);
  3. //2、实现过滤器
  4. //2.1、先声明函数
  5. protected:
  6. virtual bool eventFilter(QObject *watched, QEvent *event);
  7. //2.2、重写函数
  8. bool Widget::eventFilter(QObject *watched, QEvent *event){
  9. if(watched == ui->textEdit){//2.2.1、设置你需要监听的部件
  10. if(event->type() == QEvent::KeyPress){//2.2.2、如果为键盘上按下的某个按键
  11. //测试效果
  12. QKeyEvent *keyen = static_cast<QKeyEvent*>(event);
  13. qDebug() << keyen->key();//输出按键的键值
  14. return true;
  15. }else{
  16. return false;
  17. }
  18. }
  19. return Widget::eventFilter(watched,event);//这个函数需要一直在监视所以这里是一个递归
  20. }

 效果展示:

         这里不难看出,该部件已经将按键按下的时间捕捉到,这种情况可以参考某登录见面禁止复制粘贴很类似,但是输入法没有屏蔽掉。如果是其他的控件,也是同样的做法。

5、定时器

        定时器分为QTimerEvent和QTimer这2个类

        QTimerEvent类用来描述一个定时器事件。对于一个QObject的子类,只需要使用int QObject::startTimer ( int interval )函数来开启一个定时器,这个函数需要输入一个以毫秒为单位的整数作为参数来表明设定的时间,它返回一个整型编号来代表这个定时器。当定时器溢出时就可以在timerEvent()函数中获取该定时器的编号来进行相关操作。  

        QTimer类来实现一个定时器,它提供了更高层次的编程接口,比如可以使用信号和槽,还可以设置只运行一次的定时器。 

5.1、QTimerEvent定时器实现

  1. 1、设置定时器
  2. private:
  3. int id1;    
  4. int c;//看效果的
  5. 2、设置时间
  6. c = 1;
  7. id1=startTimer(c);//1毫秒
  8. 3、函数实现
  9. protected:
  10. virtual void timerEvent(QTimerEvent *event);
  11. void Widget::timerEvent(QTimerEvent *event){
  12. if(event->timerId() == id1){
  13. qDebug()<<id1;
  14. c+=1;
  15. id1 = startTimer(c);
  16. }
  17. }

效果展示

 5.2、QTimer定时器实现

  1. //1、设置定时器
  2. private:
  3. QTimer t1;    
  4. QTimer t2;
  5. //2、设置时间
  6. t1.start(1000);//1000毫秒    
  7. t2.start(2000);
  8. /*3、关联信号和槽
  9. 槽函数,指的是这个时间到了要执行什么函数
  10. */
  11. private slots:    
  12. void qDebugSlot1();    
  13. void qDebugSlot2();
  14. void Widget::qDebugSlot1(){    
  15. qDebug()<<"t1";
  16. }
  17. void Widget::qDebugSlot2(){    
  18. qDebug()<<"t2";
  19. }
  20. connect(&t1,SIGNAL(timeout()),this,SLOT(qDebugSlot1()));   connect(&t2,SIGNAL(timeout()),this,SLOT(qDebugSlot2()));

6、随机数种子

         在使用qrand()函数产生随机数之前,一般要使用qsrand()函数为其设置初值,如果不设置初值,那么每次运行程序,qrand()都会产生相同的一组随机数。为了每次运行程序时,都可以产生不同的随机数,我们要使用qsrand()设置一个不同的初值。

  1. qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));//设置随机数种子
  2. int rand = qrand() % 300;//获取随机数

7、拖拽事件

         就是可以直接拖放文件到程序中打开。

        例:拖入一个文本到程序中,把这个文本里面的内容都获取到程序上

  1. 包含头文件,用的wideget窗口
  2. #include <QDragEnterEvent>
  3. #include <QDropEvent>
  4. #include <QMimeData>
  5. 1、函数声明:
  6. protected:
  7. virtual void dragEnterEvent(QDragEnterEvent *event); //拖拽事件
  8. virtual void dropEvent(QDropEvent *event);//放下事件
  9. 2、函数实现
  10. void Widget::dragEnterEvent(QDragEnterEvent *event)
  11. {
  12. //判断小部件上有没有事件
  13. if(event->mimeData()->hasUrls())
  14. {
  15. event->acceptProposedAction(); //事件给过去
  16. }else{
  17. event->ignore(); //忽略
  18. }
  19. }
  20. void Widget::dropEvent(QDropEvent *event)
  21. {
  22. const QMimeData*mimeData=event->mimeData();
  23. if(!mimeData->hasUrls()){return;}
  24. QList<QUrl> urlList=mimeData->urls();//有多少个文件被拖过来
  25. for(int i=0;i<urlList.size();i++)//遍历
  26. {
  27. qDebug()<<urlList.at(i).toLocalFile();
  28. }
  29. QFile file(urlList.at(0).toLocalFile());//获取第一个文件的内容
  30. file.open(QIODevice::ReadOnly);
  31. QByteArray ba;
  32. ba=file.readAll();
  33. ui->textEdit_2->setPlainText(QString::fromLocal8Bit(ba));
  34. }
  35. 3、接受事件
  36. this->setAcceptDrops(true); //接受拖拽事件  
  37. 这个指的是窗口的,在这个窗口上面有一个多行纯文本控件

效果展示 

拓(设置右键菜单栏):

  1. 1、定义成员变量
  2. private:
  3. QMenu *popMenu; //右键弹出式菜单
  4. QAction *adjustTimeAction; //右键弹出式菜单中的内容 调整功能1
  5. QAction *adjustDateAction; //功能2
  6. QAction *quitAction; //退出
  7. 2、函数声明
  8. protected:
  9. virtual void contextMenuEvent(QContextMenuEvent *event);
  10. 3、函数实现
  11. void Widget::contextMenuEvent(QContextMenuEvent *event){ //右键会触发这个事件
  12. popMenu->clear();
  13. popMenu->addAction(adjustTimeAction);
  14. popMenu->addAction(adjustDateAction);
  15. popMenu->addSeparator();//添加线
  16. popMenu->addAction(quitAction);
  17. //popMenu->exec(); //堵住
  18. popMenu->exec(QCursor::pos());
  19. }
  20. 4、关联信号与槽以及设置(在ui->setupUi(this);之后)
  21. popMenu = new QMenu(this);
  22. popMenu->setStyleSheet(QStringLiteral("background-color: rgb(99, 99, 99);"));
  23. adjustTimeAction = new QAction(this);
  24. adjustTimeAction->setText(("功能1"));
  25. adjustDateAction = new QAction(this);
  26. adjustDateAction->setText(("功能2"));
  27. quitAction = new QAction(this);
  28. quitAction->setText(("退出"));
  29. connect(quitAction, SIGNAL(triggered(bool)),this, SLOT(close()));//关联退出的Action

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/839003
推荐阅读
相关标签
  

闽ICP备14008679号