当前位置:网站首页>[QT] QT event handling
[QT] QT event handling
2022-06-24 22:35:00 【Half raw melon blog】
Event handling
QT in , Event as an object , Inherited from QEvent class , Common keyboard events QKeyEvent、 Mouse events QMouseEvent And timer events QTimerEvent etc. .QT in , whatever QObject Subclass examples can receive and process events . In the actual programming, we usually realize the paintEvent()、mousePressEvent() And other event handling functions to handle specific events of specific parts .
Of every program main Functions are called at the end QApplication Class exec() function , It will make QT The application goes to The event loop , Make the application run Receive various events . Once something happens ,QT A corresponding QEvent Subclass object to represent it , And pass it on to QObject Object or subobject .

Mouse events
Rewrite the mouse implementation to achieve the functions you want to achieve .
mouseevent.h
#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H
#include <QMainWindow>
#include<QLabel>
#include<QMouseEvent>
namespace Ui {
class MouseEvent;
}
class MouseEvent : public QMainWindow
{
Q_OBJECT
public:
explicit MouseEvent(QWidget *parent = 0);
~MouseEvent();
protected:
void mousePressEvent(QMouseEvent *event);// The mouse click
void mouseMoveEvent(QMouseEvent *event);// Mouse movement
void mouseReleaseEvent(QMouseEvent *event);// Mouse release
private:
Ui::MouseEvent *ui;
QLabel *m_statusLabel;
QLabel* m_posLabel;
};
#endif // MOUSEEVENT_H
mouseevent.cpp
#include "mouseevent.h"
#include "ui_mouseevent.h"
MouseEvent::MouseEvent(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MouseEvent)
{
ui->setupUi(this);
setWindowTitle(QString(" Mouse events "));
m_statusLabel = new QLabel(QString(" The current position :"));
m_statusLabel->setFixedWidth(100);
m_posLabel = new QLabel(QString(""));
m_posLabel->setFixedWidth(100);
statusBar()->addPermanentWidget(m_statusLabel);// Add permanent parts to the status bar
statusBar()->addPermanentWidget(m_posLabel);
}
MouseEvent::~MouseEvent()
{
delete ui;
}
void MouseEvent::mousePressEvent(QMouseEvent *event)
{
QString str = "("+QString::number(event->x())+","+QString::number(event->y())+")";
if(event->button() == Qt::LeftButton)
{
statusBar()->showMessage(QString(" left-click :") +str );
}
else if(event->button() == Qt::MidButton)
{
statusBar()->showMessage(QString(" In the key :")+str);
}
else if(event->button() == Qt::RightButton)
{
statusBar()->showMessage(QString(" Right click ")+str);
}
}
void MouseEvent::mouseMoveEvent(QMouseEvent *event)
{
QString strPos;
strPos = "(" + QString::number(event->x())+","+QString::number(event->y())+")";
m_posLabel->setText(strPos);
}
void MouseEvent::mouseReleaseEvent(QMouseEvent *event)
{
QString strPos;
strPos = "(" + QString::number(event->x())+","+QString::number(event->y())+")";
statusBar()->showMessage(QString(" Release in :")+strPos,3000);
}

Keyboard events
Rewrite the keyboard events to achieve the desired effect .
keyevent.h
#ifndef KEYEVENT_H
#define KEYEVENT_H
#include <QWidget>
#include<QKeyEvent>
namespace Ui {
class KeyEvent;
}
class KeyEvent : public QWidget
{
Q_OBJECT
public:
explicit KeyEvent(QWidget *parent = 0);
~KeyEvent();
void drawPix();
void keyPressEvent(QKeyEvent *event) override;
void paintEvent(QPaintEvent* event)override;
private:
Ui::KeyEvent *ui;
QPixmap *m_pix;
QImage m_image;
int m_startX; // Icon vertex position
int m_startY;
int m_width; // The width of the interface , Height
int m_height;
int m_step; // step
};
#endif // KEYEVENT_H
keyevent.cpp
#include "keyevent.h"
#include "ui_keyevent.h"
#include<QPainter>
#include<QPen>
KeyEvent::KeyEvent(QWidget *parent) :
QWidget(parent),
ui(new Ui::KeyEvent)
{
ui->setupUi(this);
setWindowTitle(QString(" Keyboard events "));
setAutoFillBackground(true);
setFixedSize(521,256);
m_width = size().width();
m_height = size().height();
m_pix = new QPixmap(m_width,m_height);
m_pix->fill(Qt::white);
m_image.load("picture.jpg");
m_startX = 100;
m_startY = 100;
m_step = 20;
drawPix();
}
KeyEvent::~KeyEvent()
{
delete ui;
}
void KeyEvent::drawPix()
{
m_pix->fill(Qt::white);
QPainter painter(this);
QPen pen(Qt::DotLine);
// Draw vertical gridlines in steps
//bagin And end Pairs appear
for(int i = m_step;i < m_width; i+=m_step)
{
painter.begin(m_pix);// Appoint m_pix For drawing equipment
painter.setPen(pen);// Set pen
painter.drawLine(QPoint(i,0),QPoint(i,height()));
painter.end();
}
// Draw horizontal gridlines in steps
for(int j = m_step;j < m_height;j += m_step)
{
painter.begin(m_pix);// Appoint m_pix For drawing equipment
painter.setPen(pen);// Set pen
painter.drawLine(QPoint(0,j),QPoint(m_width,j));
painter.end();
}
// Draw pictures
painter.begin(m_pix);
painter.drawImage(QPoint(m_startX,m_startY),m_image);
painter.end();
}
void KeyEvent::keyPressEvent(QKeyEvent *event)
{
// Press down ctrl, Every move is 1 Pixel
if(event->modifiers() == Qt::ControlModifier)
{
if(event->key() == Qt::Key_Left)
{
m_startX = (m_startX -1)<0 ? m_startX : m_startX-1;
}
if(event->key() == Qt::Key_Right)
{
m_startX = (m_startX +1+m_image.width()) > m_width ? m_startX :m_startX+1 ;
}
if(event->key() == Qt::Key_Up)
{
m_startY = (m_startY - 1) < 0 ? m_startY:m_startY -1;
}
if(event->key() == Qt::Key_Down)
{
m_startY = (m_startY +1 +m_image.height()) > m_height ? m_startY:m_startY+1;
}
}
else// I didn't press ctrl key , Each move is a step
{
// Adjust the upper left corner of the icon to the vertex of the mesh
m_startX = m_startX - m_startX % m_step;
m_startY =m_startY - m_startY % m_step;
if(event->key() == Qt::Key_Left)
{
m_startX = (m_startX -m_step)<0 ? m_startX : m_startX-m_step;
}
if(event->key() == Qt::Key_Right)
{
m_startX = (m_startX +m_step+m_image.width()) > m_width ? m_startX :m_startX+m_step ;
}
if(event->key() == Qt::Key_Up)
{
m_startY = (m_startY - m_step) < 0 ? m_startY:m_startY -m_step;
}
if(event->key() == Qt::Key_Down)
{
m_startY = (m_startY +m_step +m_image.height()) > m_height ? m_startY:m_startY+m_step;
}
}
drawPix();// According to the adjusted icon position, reset the icon in m_pix Draw an image on
update();// Trigger window redrawing
}
void KeyEvent::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.drawPixmap(QPoint(0,0),*m_pix);
painter.end();
}

Event filtering
Specify what events an object handles .
** Example :** Press the mouse button to zoom the specified picture
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include<QLabel>
#include<QImage>
#include<QHBoxLayout>
#include<QEvent>
#include<QMouseEvent>
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0,Qt::WindowFlags f = 0);
~Dialog();
public slots:
bool eventFilter(QObject*watched ,QEvent *event)override;
private:
QLabel *m_label1;
QLabel *m_label2;
QLabel *m_label3;
QLabel *m_stateLabel;
QImage m_image1;
QImage m_image2;
QImage m_image3;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include<QHBoxLayout>
Dialog::Dialog(QWidget *parent,Qt::WindowFlags f)
: QDialog(parent,f)
{
setWindowTitle(QString(" Event filtering "));
m_label1 = new QLabel;
m_label2 =new QLabel;
m_label3 = new QLabel;
m_stateLabel = new QLabel(QString(" Press the mouse button to mark "));
m_stateLabel->setAlignment(Qt::AlignHCenter);// Horizontal center
m_image1.load("fly1.png");
m_image2.load("fly2.png");
m_image3.load("fly3.png");
m_label1->setPixmap(QPixmap::fromImage(m_image1));
m_label2->setPixmap(QPixmap::fromImage(m_image2));
m_label3->setPixmap(QPixmap::fromImage(m_image3));
// Add horizontal layout
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_label1);
layout->addWidget(m_label2);
layout->addWidget(m_label3);
QVBoxLayout*mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(layout);
mainLayout->addWidget(m_stateLabel);
resize(m_image2.width() * 3,m_image2.height() *2);
// Install event filtering for the picture label part , And specify the whole form as the object for monitoring events .
m_label1->installEventFilter(this);
m_label2->installEventFilter(this);
m_label3->installEventFilter(this);
}
Dialog::~Dialog()
{
}
// object - event
bool Dialog::eventFilter(QObject *watched, QEvent *event)
{
QMatrix matrix;// enlarge scale
QImage tmpImg;// Save the processed image
// Enlarge picture
matrix.scale(2.0,2.0);
// object
if(watched == m_label1)
{
// Handling of mouse down events
if(event->type() == QEvent::MouseButtonPress)
{
// Convert event types Wie Mouse events
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if(mouseEvent->button() & Qt::LeftButton)
{
m_stateLabel->setText(QString(" Left click on the picture 1"));
}
if(mouseEvent->button() & Qt::MidButton)
{
m_stateLabel->setText(QString(" Middle press the picture 1"));
}
if(mouseEvent->button() & Qt::RightButton)
{
m_stateLabel->setText(QString(" Right click on the picture 1"));
}
// Zoom the original image with tmpImg Temporary storage , Set pictures
tmpImg = m_image1.transformed(matrix);
m_label1->setPixmap(QPixmap::fromImage(tmpImg));
}
// Mouse release , Restore picture size
if(event->type() == QEvent::MouseButtonRelease)
{
m_stateLabel->setText(QString(" Mouse release picture 1"));
m_label1->setPixmap(QPixmap::fromImage(m_image1));
}
}
else if(watched ==m_label2)
{
if(event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if(mouseEvent->button() & Qt::LeftButton)
{
m_stateLabel->setText(QString(" Left click on the picture 2"));
}
if(mouseEvent->button() & Qt::MidButton)
{
m_stateLabel->setText(QString(" Middle press the picture 2"));
}
if(mouseEvent->button() & Qt::RightButton)
{
m_stateLabel->setText(QString(" Right click on the picture 2"));
}
tmpImg = m_image2.transformed(matrix);
m_label2->setPixmap(QPixmap::fromImage(tmpImg));
}
if(event->type() == QEvent::MouseButtonRelease)
{
m_stateLabel->setText(QString(" Mouse release picture 2"));
m_label2->setPixmap(QPixmap::fromImage(m_image2));
}
}
else if(watched ==m_label3)
{
if(event->type() == QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if(mouseEvent->button() & Qt::LeftButton)
{
m_stateLabel->setText(QString(" Left click on the picture 3"));
}
if(mouseEvent->button() & Qt::MidButton)
{
m_stateLabel->setText(QString(" Middle press the picture 3"));
}
if(mouseEvent->button() & Qt::RightButton)
{
m_stateLabel->setText(QString(" Right click on the picture 3"));
}
tmpImg = m_image3.transformed(matrix);
m_label3->setPixmap(QPixmap::fromImage(tmpImg));
}
if(event->type() == QEvent::MouseButtonRelease)
{
m_stateLabel->setText(QString(" Mouse release picture 3"));
m_label3->setPixmap(QPixmap::fromImage(m_image3));
}
}
// The event is handed over to the upper dialog box for processing
return QDialog::eventFilter(watched,event);
}

边栏推荐
- Basic principles of spanning tree protocol
- Problèmes de concurrence dans l'allocation de mémoire en tas
- 60 divine vs Code plug-ins!!
- Principles of Ethernet port mirroring, link aggregation and VLAN Technology
- 虚拟人的产业发展现状
- 【軟件工程】期末重點
- 问题求解——嵌套列表
- Disk structure
- Selection and comparison of message oriented middleware MQ
- Zero code can apply data visualization to enterprise management
猜你喜欢
随机推荐
Industrial development status of virtual human
堆內存分配的並發問題
Heavyweight! Fada is listed as a "specialized and new" enterprise
Selection and comparison of message oriented middleware MQ
Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?
Common voting governance in Dao
Heartless sword Chinese English bilingual poem 003 The sea of books
Unable to use the bean introduced into the jar package
What aspects should we start with in the feasibility analysis of dry goods?
Data center basic network platform
如何提取网页中的日期?
Detailed explanation of agency mode
重磅!法大大上榜“专精特新”企业
KT6368A蓝牙双模透传芯片软件版本选型说明
CDN principle
Fanuc robot_ Introduction to Karel programming (1)
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
Concurrency of heap memory allocation
Disk structure
【軟件工程】期末重點








