Qt跟MFC一样有事件机制,系统产生的事件进入Qt 窗体的事件队列,再由容器层层分发,回调QWidget子类的 xxxEvent事件函数。

// qwidget.h
// ...
protected:
  // Event handlers
  virtual void mousePressEvent(QMouseEvent *event) override; // 鼠标按下事件(不分左右) ,双击鼠标时不会触发第二次
  virtual void mouseReleaseEvent(QMouseEvent *event) override; // 鼠标释放事件(不分左右)
  virtual void mouseDoubleClickEvent(QMouseEvent *event) override; // 鼠标双击事件
  virtual void mouseMoveEvent(QMouseEvent *event) override; // 鼠标按下后的移动事件,鼠标释放时不触发
#if QT_CONFIG(wheelevent)
  virtual void wheelEvent(QWheelEvent *event) override; // 鼠标滚轮事件
#endif
  virtual void keyPressEvent(QKeyEvent *event) override; // 键盘按下事件
  virtual void keyReleaseEvent(QKeyEvent *event) override; // 键盘释放事件
  virtual void focusInEvent(QFocusEvent *event) override; // 获得焦点
  virtual void focusOutEvent(QFocusEvent *event) override; // 释放焦点
  virtual void enterEvent(QEvent *event) override; // 鼠标进入
  virtual void leaveEvent(QEvent *event) override; // 鼠标离开
  virtual void paintEvent(QPaintEvent *event) override; // 窗口重绘
  virtual void moveEvent(QMoveEvent *event) override; // 窗口移动
  virtual void resizeEvent(QResizeEvent *event) override; // 窗口改变大小
  virtual void closeEvent(QCloseEvent *event) override; // 窗口关闭
#ifndef QT_NO_CONTEXTMENU
  virtual void contextMenuEvent(QContextMenuEvent *event) override; // 鼠标右键菜单
#endif
#if QT_CONFIG(tabletevent)
  // 用于平板设备的事件
  virtual void tabletEvent(QTabletEvent *event) override;
#endif
#ifndef QT_NO_ACTION
  virtual void actionEvent(QActionEvent *event) override;
#endif

#ifndef QT_NO_DRAGANDDROP
  /**
   * 拖拽事件,一般不应用于窗体,用于控件
   */
  virtual void dragEnterEvent(QDragEnterEvent *event) override;
  virtual void dragMoveEvent(QDragMoveEvent *event) override;
  virtual void dragLeaveEvent(QDragLeaveEvent *event) override;
  virtual void dropEvent(QDropEvent *event) override;
#endif

  virtual void showEvent(QShowEvent *event) override; // 窗体显示在屏幕上时
  virtual void hideEvent(QHideEvent *event) override; // 窗体从屏幕上消失时,(包括最小化,不包括被遮挡)
  virtual bool nativeEvent(const QByteArray &eventType, void *message,
                           long *result) override;

  // Misc. protected functions
   //   窗体的变化都会触发
  virtual void changeEvent(QEvent *) override;

  // 输入法事件
  virtual void inputMethodEvent(QInputMethodEvent *) override;
// ...
  • 一个拖拽button移动的例子
// mybutton.h

#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QObject>
#include <QPushButton>
#include <QWidget>
#include <QPoint>

class MyButton : public QPushButton {
  Q_OBJECT
public:
  MyButton(QWidget *parent = nullptr);

protected:
  void mouseMoveEvent(QMouseEvent *event) override;
  void mousePressEvent(QMouseEvent *event) override;
  void mouseReleaseEvent(QMouseEvent *event) override;

private:
  QPoint prev_pos;
};

#endif // MYBUTTON_H

// mybutton.cc
#include "mybutton.h"
#include <QDebug>
#include <QMouseEvent>

MyButton::MyButton(QWidget *parent) : QPushButton(parent) {
  this->setText("drag me");
  this->show();
}

void MyButton::mouseMoveEvent(QMouseEvent *event) {
  auto delta_pos = event->globalPos() - prev_pos;
  this->move(this->pos() + delta_pos);
  this->prev_pos = event->globalPos();
}

void MyButton::mousePressEvent(QMouseEvent *event) {
  this->prev_pos = event->globalPos();
}

void MyButton::mouseReleaseEvent(QMouseEvent *event) {}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐