赞
踩
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QLineEdit> //包含头文件
class MyLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit MyLineEdit(QWidget * parent = 0);
protected:
void keyPressEvent(QKeyEvent * event); //按键按下事件声明
};
#endif // MYLINEEDIT_H
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
MyLineEdit::MyLineEdit(QWidget *parent):QLineEdit(parent)
{
}
void MyLineEdit::keyPressEvent(QKeyEvent *event) //键盘按下事件
{
qDebug()<<tr("MyLineEdit键盘按下事件");
QLineEdit::keyPressEvent(event); //执行QLineEdit类的默认事件处理
event->ignore(); //忽略该事件
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class MyLineEdit;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
MyLineEdit *lineEdit;
protected:
void keyPressEvent(QKeyEvent * event);
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "mylineedit.h"
#include <QKeyEvent>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
lineEdit=new MyLineEdit(this);
lineEdit->move(100,100);
lineEdit->installEventFilter(this); //在Widget上为lineEdit安装事件过滤器
}
Widget::~Widget()
{
delete ui;
}
void Widget::keyPressEvent(QKeyEvent *event)
{
qDebug()<<tr("Widget键盘按下事件");
}
bool Widget::eventFilter(QObject *obj, QEvent *event) //事件过滤器
{
if(obj == lineEdit)
{
if(event->type()==QEvent::KeyPress)
qDebug()<<tr("Widget的事件过滤器");
}
return QWidget::eventFilter(obj,event);
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QPoint offset; //用来存储鼠标指针位置与窗口位置的差值
protected:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QCursor cursor; //创建光标对象
cursor.setShape(Qt::OpenHandCursor); //设置光标形状(小手形状的光标--松开的手)
setCursor(cursor); //使用光标
//如果想不安鼠标按键,也可以获取鼠标移动事件,就添加下面这行代码
setMouseTracking(true); //设置鼠标跟踪
}
Widget::~Widget()
{
delete ui;
}
//鼠标按下事件
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
QCursor cursor;
cursor.setShape((Qt::ClosedHandCursor));
QApplication::setOverrideCursor(cursor); //使用鼠标指针暂时改变形状(攥紧的小手)
offset=event->globalPos()-pos(); //获取指针位置和窗口位置的差值
}
else if(event->button()==Qt::RightButton)
{
QCursor cursor(QPixmap("../mymouseevent/logo.png"));
QApplication::setOverrideCursor(cursor); //使用自定义的图片作为鼠标指针
}
}
//鼠标释放事件
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event) //这样编译时就不会出现警告了
QApplication::restoreOverrideCursor(); //恢复鼠标指针形状
}
//鼠标双击事件
void Widget::mouseDoubleClickEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(windowState()!=Qt::WindowFullScreen)
setWindowState(Qt::WindowFullScreen); //全屏
else
setWindowState(Qt::WindowNoState); //恢复以前的大小
}
}
//鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton) //这里必须使用buttons
{
QPoint temp;
temp=event->globalPos()-offset;
move(temp); //使用鼠标指针当前的位置减去差值,就得到了窗口应该移动的位置
}
}
//滚轮事件
void Widget::wheelEvent(QWheelEvent *event)
{
if(event->delta()>0) //当滚轮远离使用者时
{
ui->textEdit->zoomIn(); //放大
}
else //当滚轮走向使用者时
{
ui->textEdit->zoomOut();//缩小
}
//每当滚轮滚动一下,默认是15°,delta()函数会返回15*18即120.
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
bool keyUp; //向上方向键按下的标志
bool keyLeft; //向左方向键按下的标志
bool move; //是否完成了一次移动
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFocus(); //这一句千万别落下
keyUp = false;
keyLeft = false;
move = false;
}
Widget::~Widget()
{
delete ui;
}
//键盘按下事件
void Widget::keyPressEvent(QKeyEvent *event)
{
if(event->modifiers()==Qt::ControlModifier) //是否按下Ctrl键
{
if(event->key()==Qt::Key_M) //是否按下M键
setWindowState(Qt::WindowMaximized); //窗口最大化
}
else if(event->key() == Qt::Key_Up){ // 如果是向上方向键
qDebug() << "press:"<<event->isAutoRepeat(); // 是否自动重复
}
if(event->key()==Qt::Key_Up)
{
if(event->isAutoRepeat())
return; //按键重复时不做处理
keyUp = true;
}
if (event->key() == Qt::Key_Up)
{
if(event->isAutoRepeat())
return; // 按键重复时不做处理
}
else if(event->key()==Qt::Key_Left)
{
if(event->isAutoRepeat())
return; //按键重复时不做处理
keyLeft = true;
}
else
{
QWidget::keyPressEvent(event);
}
}
//键盘释放事件
void Widget::keyReleaseEvent(QKeyEvent *event)
{
if(event->modifiers()==Qt::ControlModifier) //是否按下Ctrl键
{
if(event->key()==Qt::Key_M) //是否按下M键
setWindowState(Qt::WindowNoState); //窗口恢复原来大小
}
else if(event->key()==Qt::Key_Up)
{
if(event->isAutoRepeat())
return;
keyUp = false; //释放按键后将标志设置为false
if(move) //如果已经完成了移动
{
move = false; //设置标志位false
return; //直接返回
}
if(keyLeft) //如果向左方向键已经按下且没有释放
{
ui->pushButton->move(30,80); //斜移
qDebug()<<"斜移";
move = true;
}
else
{
ui->pushButton->move(120,80); //上移
qDebug()<<"上移";
}
}
else if(event->key()==Qt::Key_Left)
{
if(event->isAutoRepeat()) return;
keyLeft = false;
if(move)
{
move=false;
return;
}
if(keyUp)
{
ui->pushButton->move(30,80);
move = true;
}
else
{
ui->pushButton->move(30,120);
qDebug()<<"左移";
}
}
else if(event->key()==Qt::Key_Down)
{
ui->pushButton->move(120,120); //使用向下方向键还原按钮的位置
}
else
{
QWidget::keyReleaseEvent(event);
}
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
int id1,id2,id3;
private slots:
void timerUpdate();
protected:
void timerEvent(QTimerEvent *event);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTimer>
#include <QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
id1 = startTimer(1000); //开启一个定时1s的定时器,返回ID
id2 = startTimer(1500); //startTimer()函数参数是以毫秒为单位的
id3 = startTimer(2200);
QTimer * timer = new QTimer(this);
connect(timer,&QTimer::timeout,this,&Widget::timerUpdate);
timer->start(1000);
//随机数
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
//singleShot()函数开启一个只运行一次的定时器,下面代码让程序运行5s后自动关闭
QTimer::singleShot(5000,this,&Widget::close);
}
Widget::~Widget()
{
delete ui;
}
//定时器溢出处理
void Widget::timerUpdate()
{
QTime time = QTime::currentTime(); //获取当前时间
QString text = time.toString("hh:mm"); //转化为字符
if((time.second()%2)==0) //每个1s, ":"会显示为空格,从而形成闪烁的效果
text[2]=' ';
ui->lcdNumber->display(text);
int rand = qrand()%300; //产生300以内的正整数
ui->lcdNumber->move(rand,rand);
}
void Widget::timerEvent(QTimerEvent *event)
{
if(event->timerId()==id1)
{
qDebug()<<"timer1";
}
else if(event->timerId()==id2)
{
qDebug()<<"timer2";
}
else if(event->timerId()==id3)
{
qDebug()<<"timer3";
}
}
下面例程完成以下功能:
编辑widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
protected:
bool eventFilter(QObject * obj,QEvent * event);
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include <QWheelEvent>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->textEdit->installEventFilter(this);
ui->spinBox->installEventFilter(this);
}
Widget::~Widget()
{
delete ui;
}
//事件过滤器
bool Widget::eventFilter(QObject *obj, QEvent *event)
{
if(obj==ui->textEdit)
{
if(event->type()==QEvent::Wheel)
{
//将event强制转换为发生的事件的类型
QWheelEvent * wheelEvent = static_cast<QWheelEvent *>(event);
if(wheelEvent->delta()>0)
ui->textEdit->zoomIn();
else
ui->textEdit->zoomOut();
return true; //该事件已经处理过
}
else
return false; //如果是其他事件,可以进行进一步处理
}
else if(obj==ui->spinBox)
{
if(event->type()==QEvent::KeyPress)
{
QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
if(keyEvent->key()==Qt::Key_Space)
{
ui->spinBox->setValue(0);
return true;
}
else
return false;
}
else
return false;
}
else
return QWidget::eventFilter(obj,event);
//如果对一个事件进行处理,而且不希望它在后面的传递过程中再被处理,那么就返回true,否则返回false
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。