diff --git a/SoftwareDesign/Code/2-3-4/mainwindow.cpp b/SoftwareDesign/Code/2-3-4/mainwindow.cpp index 41a26bd..0e7b38a 100644 --- a/SoftwareDesign/Code/2-3-4/mainwindow.cpp +++ b/SoftwareDesign/Code/2-3-4/mainwindow.cpp @@ -2,14 +2,39 @@ #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) - , ui(new Ui::MainWindow) -{ - ui->setupUi(this); + : QMainWindow(parent), ui(new Ui::MainWindow) { + ui->setupUi(this); + ui->centralwidget->setMouseTracking(true); + setMouseTracking(true); } -MainWindow::~MainWindow() -{ - delete ui; -} +MainWindow::~MainWindow() { 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"); +} diff --git a/SoftwareDesign/Code/2-3-4/mainwindow.h b/SoftwareDesign/Code/2-3-4/mainwindow.h index f904d83..5e927f1 100644 --- a/SoftwareDesign/Code/2-3-4/mainwindow.h +++ b/SoftwareDesign/Code/2-3-4/mainwindow.h @@ -2,8 +2,8 @@ #define MAINWINDOW_H #include -#include #include +#include QT_BEGIN_NAMESPACE namespace Ui { @@ -22,6 +22,10 @@ private: Ui::MainWindow *ui; 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 diff --git a/SoftwareDesign/Code/2-3-4/mainwindow.ui b/SoftwareDesign/Code/2-3-4/mainwindow.ui index ac966a0..ad126f4 100644 --- a/SoftwareDesign/Code/2-3-4/mainwindow.ui +++ b/SoftwareDesign/Code/2-3-4/mainwindow.ui @@ -14,12 +14,12 @@ 4490 - + - 110 + 450 160 - 77 + 311 25 @@ -27,12 +27,12 @@ TextLabel - + - 330 + 160 160 - 77 + 221 25