void QWidget::keyPressEvent(QKeyEvent* event);
bool QWidget::event(QEvent* event);//返回true表示结束
QEvent 是所有QT事件的基类,而QKeyEvent是QEvent的派生类,用于描述按键事件。
常用接口如下:
int QKeyEvent::key() 返回按键事件的按键编码
QString QKeyEvent::text() 将按键事件转化为字符串
例子:
label标签中实现键盘输入即时显示
头文件加入:
//头文件加入:
#include
//Widget类加入:
private:
Ui::Widget *ui;
virtual void keyPressEvent(QKeyEvent*);
源文件加入:
void QWidget::keyPressEvent(QKeyEvent* event)
{
//ctrl+q
if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Q)
{
this->close();
return;
}
QString text = ui->label->text();
if(event->text() == "\b") text.chop(1);
else text += event->text();
ui->label->setText(text);
}
void QWidget::mousePressEvent(QMouseEvent * event);
void QWidget::mouseMoveEvent(QMouseEvent * event);
void QWidget::mouseReleaseEvent(QMouseEvent * event);
bool QWidget::event(QEvent* event);
//相对于!!电脑屏幕!!左上角的X坐标(右为正方向)
int QMouseEvent::globalX()
//相对于电脑屏幕左上角的Y坐标(下为正方向)
int QMouseEvent::globalY()
//相对于!!控件!!左上角的X坐标
int QMouseEvent::x()
//相对于控件左上角的Y坐标
int QMouseEvent::y()
示例:
#ifndef MYFRAME_H
#define MYFRAME_H
#include
class myframe : public QFrame
{
Q_OBJECT
public:
explicit myframe(QWidget *parent=0);
protected:
virtual void mousePressEvent(QMouseEvent *ev);
virtual void mouseMoveEvent(QMouseEvent *ev);
virtual void mouseReleaseEvent(QMouseEvent *ev);
signals:
public slots:
};
#endif // MYFRAME_H
源文件:
#include "myframe.h"
#include
#include
myframe::myframe(QWidget *parent):QFrame(parent)
{
}
void myframe::mousePressEvent(QMouseEvent *ev)
{
//when click show mouse position
qDebug()<<ev->x()<<","<<ev->y(); //pos in widget
// qDebug()<globalX()<<","<globalY(); //pos in screen
}
void myframe::mouseMoveEvent(QMouseEvent *ev)
{
}
void myframe::mouseReleaseEvent(QMouseEvent *ev)
{
}
void QWidget::paintEvent(QPaintEvent *event);
在绘图事件中可以使用QPainter来对控件进行重绘,重用接口如下:
构造函数:
QPainter::QPainter()
绘图前准备工作,device为要绘制的控件对象地址
bool QPainter::begin(QPaintDevice *device)
绘图结束
bool QPainter::end()
示例1:绘制一段线
#include
void myframe::paintEvent(QPaintEvent *ev)
{
QPainter painter;
painter.begin(this);
//set point
QPoint start(0,0);
QPoint end(100,100);
//draw
painter.drawLine(start,end);
painter.end();
}
示例2:鼠标绘画
头文件
#include
#include
#include
using namespace std;
class myframe : public QFrame
{
Q_OBJECT
private:
vector<QPoint> points;
源文件
#include "myframe.h"
#include
#include
#include
myframe::myframe(QWidget *parent):QFrame(parent)
{
}
void myframe::mousePressEvent(QMouseEvent *ev)
{
QPoint point(ev->x(),ev->y());
points.push_back(point);
update();
}
void myframe::mouseMoveEvent(QMouseEvent *ev)
{
QPoint point(ev->x(),ev->y());
points.push_back(point);
update();
}
void myframe::mouseReleaseEvent(QMouseEvent *ev)
{
QPoint point(ev->x(),ev->y());
points.push_back(point);
update();
}
void myframe::paintEvent(QPaintEvent *ev)
{
QPainter painter;
vector<QPoint>::iterator it;
if(points.size()<=1)return;
painter.begin(this);
QPoint start = points[0];
for(it=points.begin();it!=points.end();it++)
{
painter.drawLine(start,*it);
start = *it;
}
painter.end();
}
QT事件派发机制
spontaneous Event->消息队列->事件派发
Posted Event-> 消息队列->事件派发
sent Event->事件派发
事件派发->事件过滤器->事件函数(keyPressEvent等)
QT事件过滤器
bool QObject::eventFilter(QObject *watch, QEvent* event);
watched:产生经过过滤器的事件的对象
event:经过过滤器的事件对象
返回值:过滤的事件是否结束
当类改写过QObject::eventFilter后,该类的对象就可以被安装到其他对象上进行事件过滤,安装的接口如下:
void QObject::installEventFilter(QObject *filterObj);
filterObj: 改写过QObject::eventFilter的对象
示例:创建一个不能输入的输入框
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
mainwindow::mainwindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::mainwindow)
{
ui->setupUi(this);
ui->le_input->installEventFilter(this);
}
mainwindow::~mainwindow()
{
delete ui;
}
bool mainwindow::eventFilter(QObject* watched,QEvent* ev)
{
if(watched == ui->le_input)
{
if(ev->type() == QEvent::KeyPress)
{
qDebug()<<"keyPress filter";
return true;
}
}
return false;
}