概述
Qt事件Event
1.事件函数event
2.鼠标事件
3.键盘事件
4.窗口大小变化事件
5.重绘事件
重载event
1.bool event(QEvent *e);
2.返回true 处理下一个事件
3.返回false本事件的下一个处理函数
QEvent子类
1.QKeyEvent,QMouseEvent
2.QTouchEvent,QWheelEvent
QWidget重载函数
1.virtual void keyPressEvent(QKeyEvent *event);
2.virtual void keyReleaseEvent(QKeyEvent *event);
3.virtual void leaveEvent(QEvent* event);
4.virtual void mouseDoubleClickEvent(QMouseEvent *event);
5.virtual void mouseMoveEvent(QMouseEvent *Event);
6.virtual void mousePressEvent(QMouseEvent *Event);
7.virtual void mouseReleaseEvent(QMouseEvent *Event);
8.virtual void moveEvent(QMoveEvent *event);
9.virtual void resizeEvent(QResizeEvent *event);
10.virtual void showEvent(QShowEvent *event);
11.virtual void wheelEvent(QWheelEvent *event);
12.virtual void paintEvent(QPaintEvent *event);
QMouseEvent坐标
1.x() y() 相对(本地)坐标
2.QPoint windowPos() 程序窗口坐标
3.screenPos() 屏幕坐标
4.QWidget::mapToGlobal(ev->pos()); 屏幕
5.QCursor::pos()屏幕
QMouseEvent按键状态
1.buttons() & Qt::LeftButton
2.Qt::RightButton Qt::MidButton
3.(me->buttons() & Qt::LeftButton)
&&(me->buttons() & Qt::RightButton)
鼠标事件函数的重载
1.virtual void enterEvent(QEvent *event);
2.virtual void leaveEvent(QEvent *event);
3.virtual void mouseDoubleClickEvent(QMouseEvent *event);
4.virtual void mouseMoveEvent(QMouseEvent *event);
5.virtual void mousePressEvent(QMouseEvent *event);
6.virtual void mouseReleaseEvent(QMouseEvent *event);
xlabel.cpp
#include "xlabel.h"
#include <QDebug>
#include <QMouseEvent>
XLabel::XLabel(QWidget* parent)
: QLabel(parent)
{
}
XLabel::~XLabel()
{
}
bool XLabel::event(QEvent *ev)
{
if(ev->type() == QEvent::MouseButtonPress)
{
QMouseEvent *me = static_cast<QMouseEvent*> (ev);
//相对坐标
qDebug()<<"LocalPos "<<me->x()<<":"<<me->y();
//程序坐标
qDebug()<<"windowPos "<<me->windowPos().x()<<":"<<me->windowPos().y();
//屏幕坐标
qDebug()<<"windowPos "<<me->screenPos().x()<<":"<<me->screenPos().y();
//本地坐标转全局坐标
QPoint gpos = mapToGlobal(me->pos());
qDebug()<<"mapToGlobal "<<gpos.x()<<":"<<gpos.y();
//获取鼠标屏幕坐标
qDebug()<<"QCursor "<<QCursor::pos().x()<<":"<<QCursor::pos().y();
//鼠标按键事件
if(me->buttons() & Qt::LeftButton)
{
qDebug()<<"LeftButton";
}
if(me->buttons() & Qt::RightButton)
{
qDebug()<<"RightButton";
}
if(me->buttons() & Qt::MiddleButton)
{
qDebug()<<"MidButton";
}
//左右同时按下
if((me->buttons() & Qt::LeftButton) && (me->buttons() & Qt::RightButton))
{
qDebug()<<"LeftButton and RightButton";
}
}
return QWidget::event(ev);
}
光标的样式
QCursor cursor;
QPixmap pixmap("cursor.png");
//-1,-1表示热点取中间
cursor = QCursor(pixmap,-1,-1);
setCursor(cursor);
setCursor(Qt::ArrowCursor);
xlabel.cpp
#include "xlabel.h"
#include <QDebug>
#include <QMouseEvent>
XLabel::XLabel(QWidget* parent)
: QLabel(parent)
{
setMouseTracking(true);
}
XLabel::~XLabel()
{
}
bool XLabel::event(QEvent *ev)
{
if(ev->type() == QEvent::MouseButtonPress)
{
QMouseEvent *me = static_cast<QMouseEvent*> (ev);
//相对坐标
qDebug()<<"LocalPos "<<me->x()<<":"<<me->y();
//程序坐标
qDebug()<<"windowPos "<<me->windowPos().x()<<":"<<me->windowPos().y();
//屏幕坐标
qDebug()<<"windowPos "<<me->screenPos().x()<<":"<<me->screenPos().y();
//本地坐标转全局坐标
QPoint gpos = mapToGlobal(me->pos());
qDebug()<<"mapToGlobal "<<gpos.x()<<":"<<gpos.y();
//获取鼠标屏幕坐标
qDebug()<<"QCursor "<<QCursor::pos().x()<<":"<<QCursor::pos().y();
//鼠标按键事件
if(me->buttons() & Qt::LeftButton)
{
qDebug()<<"LeftButton";
setCursor(Qt::ArrowCursor);
}
if(me->buttons() & Qt::RightButton)
{
qDebug()<<"RightButton";
}
if(me->buttons() & Qt::MiddleButton)
{
qDebug()<<"MidButton";
}
//左右同时按下
if((me->buttons() & Qt::LeftButton) && (me->buttons() & Qt::RightButton))
{
qDebug()<<"LeftButton and RightButton";
//鼠标图标替换
QPixmap pix("vline.png");
QCursor cur = QCursor(pix,-1,-1);
setCursor(cur);
}
}
return QWidget::event(ev);
}
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include "xlabel.h"
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
XLabel *label;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new XLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(200, 200, 400, 400));
label->setStyleSheet(QString::fromUtf8("background-color: rgb(92, 53, 102);"));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QString());
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
窗口大小改变事件
void resizeEvent(QResizeEvent *event);
if(ev->type() == QEvent::Resize)
const QSize &oldSize() const;
const QSize &size() const;
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(0, 30, 800, 670));
label->setStyleSheet(QString::fromUtf8("background-color: rgb(92, 53, 102);"));
retranslateUi(Widget);
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QString());
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QResizeEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
bool Widget::event(QEvent *e)
{
if(e->type() == QEvent::Resize)
{
QResizeEvent *re = static_cast<QResizeEvent *>(e);
qDebug()<<"Resize old "<<re->oldSize().width()<<":"<<re->oldSize().height();
qDebug()<<"new size"<<re->size().width()<<":"<<re->size().height();
}
return QWidget::event(e);
}
Widget::~Widget()
{
delete ui;
}
最后
以上就是成就冰淇淋为你收集整理的Qt事件Event的全部内容,希望文章能够帮你解决Qt事件Event所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复