鼠标点击响应(左键单击,右键单击)

文件目录
test.pro
—test.h
—test.cpp
—main.cpp
—test.ui

test.h

#ifndef TEST_H
#define TEST_H

#include 
#include "ui_test.h"
#include 

class Test : public QMainWindow
{
    Q_OBJECT

public:
    Test(QWidget *parent = 0);
    ~Test();

protected:
    //鼠标点击事件
    void mousePressEvent(QMouseEvent* event);
    //鼠标移动事件
    void mouseMoveEvent(QMouseEvent *event);
    //鼠标释放事件
    void mouseReleaseEvent(QMouseEvent *event);

private:
    Ui::Test ui;
};

#endif // TEST_H

test.cpp

#include "test.h"
#include 

Test::Test(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

Test::~Test()
{

}

//鼠标点击事件
void Test::mousePressEvent(QMouseEvent* event)
{
    if(event->button()==Qt::LeftButton)
    {
        qDebug()<<"PushLeftButton!"<button()==Qt::RightButton)
    {
        qDebug()<<"PushRightButton!"<button()==Qt::MidButton)
    {
        qDebug()<<"PushMidButton!"<buttons()&Qt::LeftButton)
    {
        qDebug()<<"PushLeftButtonAndMove!"<buttons()&Qt::RightButton)
    {
        qDebug()<<"PushRightButtonAndMove!"<buttons()&Qt::MidButton)
    {
        qDebug()<<"PushMidButtonAndMove!"<button()==Qt::LeftButton)
    {
        qDebug()<<"ReleaseLeftButton!"<button()==Qt::RightButton)
    {
        qDebug()<<"ReleaseRightButton!"<button()==Qt::MidButton)
    {
        qDebug()<<"ReleaseMidButton!"<

main.cpp

#include "test.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Test w;
    w.show();

    return a.exec();
}

备注:
在窗口上右键单击的时候会打印 “ PushRightButton” ,放开会打印“ReleaseRightButton”,左键同样;打印信息在应用程序输出窗口。

你可能感兴趣的:(鼠标点击响应(左键单击,右键单击))