This commit is contained in:
iridiumR 2022-04-19 15:19:23 +08:00
parent 5595711307
commit ee2bce6697
3 changed files with 45 additions and 16 deletions

View file

@ -2,14 +2,39 @@
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
, ui(new Ui::MainWindow)
{
ui->setupUi(this); ui->setupUi(this);
ui->centralwidget->setMouseTracking(true);
setMouseTracking(true);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow() { delete ui; }
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton)
ui->l1->setText("Left button press");
else
ui->l1->setText("Right button press");
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton)
ui->l1->setText("Left button release");
else
ui->l1->setText("Right button release");
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *e) {
if (e->button() == Qt::LeftButton)
ui->l1->setText("Left button double click");
else
ui->l1->setText("Right button double click");
}
void MainWindow::mouseMoveEvent(QMouseEvent *e) {
QPoint pos = e->globalPos();
ui->l2->setText(QString("(%1,%2)").arg(pos.rx()).arg(pos.ry()));
}
void MainWindow::wheelEvent(QWheelEvent *e) {
if (e->delta() > 0)
ui->l1->setText("scroll up");
else
ui->l1->setText("scroll down");
}

View file

@ -2,8 +2,8 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include <QWheelEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QWheelEvent>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
@ -22,6 +22,10 @@ private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
protected: protected:
void mouseEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void mouseDoubleClickEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void wheelEvent(QWheelEvent *e);
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View file

@ -14,12 +14,12 @@
<string>4490</string> <string>4490</string>
</property> </property>
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label"> <widget class="QLabel" name="l2">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>110</x> <x>450</x>
<y>160</y> <y>160</y>
<width>77</width> <width>311</width>
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>
@ -27,12 +27,12 @@
<string>TextLabel</string> <string>TextLabel</string>
</property> </property>
</widget> </widget>
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="l1">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>330</x> <x>160</x>
<y>160</y> <y>160</y>
<width>77</width> <width>221</width>
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>