
今天,我了解了QT类中的键盘和鼠标事件,并编写了一个非常简单的测试应用程序。突然我发现使用C ++开发项目非常有效。您可以调用任何类,也可以继承某个类并添加自己的函数。当我用来开发图形界面和鼠标时,我用C语言开发了所有自己的滚轮,自己读取了/ dev / input / event *接口,然后处理了获取的输入数据。当时感觉太复杂了。现在只需使用QT直接获取一个类,界面效果如下:

屏幕中间的指针只能在坐标轴上移动。使用上,下,左和右键控制方向。有一个可选的复选框按钮,用于控制光标是否可以连续移动。代码如下:
main.cpp

#include
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h / *有四个未使用的变量,分别是up,down,left和right标志,保留扩展名* /
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
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
#include
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
Widget
0
0
400
300
Widget
80
130
200
16
Qt::Horizontal
180
30
16
200
Qt::Vertical
80
130
16
16
o
270
240
81
21
speed
100
0
161
21
光标在数轴上移动演示
Qt::AlignCenter
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-337966-1.html
如此说来
第三次世界大战不是梦啊
台湾同胞抗议台独