回顾:
第一章:Qt的概述
第二章:在Ubuntu编写第一个Qt程序
第三章:Qt的字符串和字符编码
第四章:Qt的信号和槽
第五章:Qt容器窗口(父窗口)
第六章:面向对象的Qt编程
第七章:Qt设计师使用(designer)
第八章:Qt创造器的使用(qtcreator)
第九章:资源和图像
第十章:目录与定时器
鼠标和键盘事件
一、鼠标事件
#include <QMouseEvent>
void mousePressEvent(QMouseEvent *)//按下
void mouseReleaseEvent(QMouseEvent)//抬起
void mouseMoveEvent(QMouseEvent *)//移动
void mouseDoubleClickEvent(QMouseEvent *)//双击
button()//获取引发鼠标事件的案件
pos()//获取鼠标点击的位置
案例:通过鼠标拖拽label
移动
- 工程名:Mouse
- 类名:MouseDialog
- 设置
label
的背景颜色:autoFillBackground
勾选√
MouseDialog.h
#ifndef MOUSEDIALOG_H
#define MOUSEDIALOG_H
#include <QDialog>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MouseDialog; }
QT_END_NAMESPACE
class MouseDialog : public QDialog
{
Q_OBJECT
public:
MouseDialog(QWidget *parent = nullptr);
~MouseDialog();
private:
//鼠标按下时被执行
void mousePressEvent(QMouseEvent *);
//鼠标抬起时被执行
void mosuseReleaseEvent(QMouseEvent *);
//鼠标移动时被执行
void mouseMoveEvent(QMouseEvent *);
private:
Ui::MouseDialog *ui;
bool m_bDrag;//标记是否左键已经选中label
QPoint m_ptPos;//记录鼠标点击和label的相对位置
};
#endif // MOUSEDIALOG_H
MuoseDialog.cpp
#include "MouseDialog.h"
#include "ui_MouseDialog.h"
MouseDialog::MouseDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::MouseDialog)
{
ui->setupUi(this);
m_bDrag = false;
}
MouseDialog::~MouseDialog()
{
delete ui;
}
//鼠标按下时被执行
void MouseDialog::mousePressEvent(QMouseEvent *event)
{
//判断是否为鼠标左键
if(event->button() == Qt::LeftButton){
//获取label矩形区域
QRect rcLabel = ui->label->frameRect();
//平移让和鼠标事件使用相同的坐标
rcLabel.translate(ui->label->pos());
//判断鼠标点击的位置是否在rcLabel矩形范围中
if(rcLabel.contains(event->pos())==true){
m_bDrag = true;
//记录鼠标和label的相对位置
m_ptPos= ui->label->pos() - event->pos();
}
}
}
//鼠标抬起时被执行
void MouseDialog::mosuseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
m_bDrag = false;
}
}
//鼠标移动时被执行
void MouseDialog::mouseMoveEvent(QMouseEvent *event)
{
if(m_bDrag == true){
//计算label移动的新位置
QPoint newPos = event->pos() + m_ptPos;
//设置限制
//获取窗口大小
QSize sz = size();
//获取label大小
QSize szLabel = ui->label->size();
//x:0--(窗口-label宽)
if(newPos.x() < 0){
newPos.setX(0);
}
else if(newPos.x() > sz.width()-szLabel.width()){
newPos.setX(sz.width()-szLabel.width());
}
//y:0--(窗口高-label高)
if(newPos.y() < 0){
newPos.setY(0);
}
else if(newPos.y() > sz.height()-szLabel.height()){
newPos.setY(sz.height()-szLabel.height());
}
//移动label到新的位置
ui->label->move(newPos);
}
}
二、键盘事件
#include <QKeyEvent>
//按键按下
void keyPressEvent(QKeyEvent *)
//按键抬起
void keyReleaseEvent(QKeyEvent *)
int key()//获取按键代码,用于区分不同的按键
QString text()//获取按键文本
案例:按键按下给出相应的反应
KeyboardDialog.h
#define KEYBOARDDIALOG_H
#include <QDialog>
#include <QKeyEvent>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class KeyboardDialog; }
QT_END_NAMESPACE
class KeyboardDialog : public QDialog
{
Q_OBJECT
public:
KeyboardDialog(QWidget *parent = nullptr);
~KeyboardDialog();
private:
//
void keyPressEvent(QKeyEvent *);
//
void keyReleaseEvent(QKeyEvent *);
private:
Ui::KeyboardDialog *ui;
};
#endif // KEYBOARDDIALOG_H
KeyboardDialog.cpp
#include "KeyboardDialog.h"
#include "ui_KeyboardDialog.h"
KeyboardDialog::KeyboardDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::KeyboardDialog)
{
ui->setupUi(this);
}
KeyboardDialog::~KeyboardDialog()
{
delete ui;
}
//
void KeyboardDialog::keyPressEvent(QKeyEvent *event)
{
qDebug("press key!");
qDebug("key code:%d", event->key());
qDebug() << "key text:" << event->text();
}
//
void KeyboardDialog::keyReleaseEvent(QKeyEvent *event)
{
qDebug("release key!");
qDebug("key code:%d", event->key());
qDebug() << "key text:" << event->text();
}