QT项目之键盘控制光标移动

今天学习了QT类中的键盘和鼠标事件,就写了一个很简单的应用程序来做测试。突然发现用C++开发项目效率好高,什么都可以调用类,或者去继承某一个类,添加自己的函数就行。以前做图形界面和鼠标开发的时候,全是自己造轮子,用C语言开发,自己去读取/dev/input/event*接口,然后去处理获取到的input数据,那时候感觉好复杂,现在用QT直接一个类就搞定,界面效果如下:




屏幕中间的指针只能在坐标轴上移动,用上下左右键控制方向,有一个checkBox可选按钮来控制光标是否可以连续移动代码如下:


main.cpp

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}

widget.h    /* 有四个变量没有用到,分别是上下左右标志位,留着扩展 */

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QKeyEvent>
#include <QDebug>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    
private:
    Ui::Widget *ui;
    bool keyUp;
    bool keyDown;
    bool keyLeft;
    bool keyRight;
    bool flag;

protected:
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QPalette>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QPalette pa;
    ui->setupUi(this);
    pa.setColor(QPalette::WindowText,Qt::red);
    ui->label->setPalette(pa);
    //setFocus();
    flag=1;   /* 看是否使用isAutoRepeat */
    keyUp=false;
    keyLeft=false;
    keyRight=false;
    keyDown=false;

    ui->pushButton->move(180,130); /* set middle point */
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    static int offset_x=0,offset_y=0;
    flag =ui->checkBox->isChecked();

    if(event->key() == Qt::Key_Up)
    {
        if(event->isAutoRepeat()&&(flag==0)) return;
        if(offset_x==0)
        {
            offset_y=offset_y-10;
            if(offset_y<-100)
            offset_y=-100;
            ui->pushButton->move(180+offset_x,130+offset_y);
        }
      }

    if(event->key() == Qt::Key_Down)
    {
        if(event->isAutoRepeat()&&(flag==0)) return;
        if(offset_x==0)
        {
            offset_y=offset_y+10;
            if(offset_y>100)
            offset_y=100;
            ui->pushButton->move(180+offset_x,130+offset_y);
        }
      }

    if(event->key() == Qt::Key_Left)
    {
        if(event->isAutoRepeat()&&(flag==0)) return;

        if(offset_y==0)
        {
            offset_x=offset_x-10;
            if(offset_x<-100)
            offset_x=-100;
            ui->pushButton->move(180+offset_x,130+offset_y);
        }
      }

    if(event->key() == Qt::Key_Right)
    {
        if(event->isAutoRepeat()&&(flag==0)) return;

        if(offset_y==0)
        {
            offset_x=offset_x+10;
            if(offset_x>100)
            offset_x=100;
            ui->pushButton->move(180+offset_x,130+offset_y);
        }
      }
}

widget.ui



<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="Line" name="line">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>130</y>
     <width>200</width>
     <height>16</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
  </widget>
  <widget class="Line" name="line_2">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>30</y>
     <width>16</width>
     <height>200</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Vertical</enum>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>130</y>
     <width>16</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>o</string>
   </property>
  </widget>
  <widget class="QCheckBox" name="checkBox">
   <property name="geometry">
    <rect>
     <x>270</x>
     <y>240</y>
     <width>81</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>speed</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>0</y>
     <width>161</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>光标在数轴上移动演示</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
Qt OpenGL是Qt框架中的一个模块,用于在Qt应用程序中使用OpenGL。它提供了一组类和函数,使得开发者可以轻松地在Qt应用中实现图形渲染和交互。 要使用Qt OpenGL实现室内移动,我们可以按照以下步骤进行: 1. 创建Qt OpenGL窗口:使用QOpenGLWidget类创建一个新的窗口,该窗口将用于显示OpenGL渲染的图形。在这个窗口中,可以自定义视图和交互方式。 2. 加载室内地图:将室内地图的数据加载到Qt应用程序中。可以使用OpenGL的纹理映射功能将地图贴到墙壁和地面上,以实现真实感的效果。 3. 实现相机控制:实现相机控制以实现室内移动。可以通过鼠标和键盘事件来控制相机的位置和朝向。例如,使用鼠标移动来改变相机的视角,使用键盘控制相机的移动方向。可以使用OpenGL的矩阵操作来实现相机控制。 4. 实现物体的渲染:根据需要,在室内地图中渲染出其他的物体,例如家具、人物等。可以使用OpenGL的3D模型加载功能来实现物体的加载和渲染。 5. 实现碰撞检测:为了使室内移动更加真实,可以实现碰撞检测功能。例如,当相机接近墙壁时,禁止相机继续前进,或者当相机碰撞到其他物体时,禁止相机继续移动。 6. 实现光照效果:为了实现更逼真的室内场景,可以添加光照效果。可以使用OpenGL的光源和材质属性来设置光照效果,例如点光源、聚光灯等。 通过以上步骤,我们可以使用Qt OpenGL实现室内移动。这样的应用程序可以用于虚拟现实、游戏开发、建筑可视化等领域,提供交互和沉浸式的体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeroKern

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值