QT之鼠标点击事件学习

最近在学习点击鼠标事件,在这分享给大家

window.h中的配置

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"Qlabel"
#include"QStatusBar"
#include <QMainWindow>
#include"QMouseEvent"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
protected:
    void mousePressEvent(QMouseEvent *e);//鼠标点击事件
    void mouseMoveEvent(QMouseEvent *e);//鼠标移动事件
    void mouseReleaseEvent(QMouseEvent *e);//鼠标释放事件
    void mouseDoubleClickEvent(QMouseEvent *e);//鼠标双击事件

private:
    Ui::MainWindow *ui;
    QLabel *statusLabel;  //控件  QLabel
    QLabel *MousePosLabel;//控件  QLabel
};

#endif // MAINWINDOW_H
mainwindow.cpp中配置
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
  //  ui(new Ui::MainWindow)
{
  //  ui->setupUi(this);
    setWindowTitle(tr("鼠标事件"));
    statusLabel=new QLabel; //创建控件  LABEL
    statusLabel->setText(tr("当前位置:"));
    //statusLabel->SetFixedXY(40,40);
    statusLabel->setFixedWidth(100);  //设置控件宽度  SetFixedXY   SetFixedHeight    SetFixedWidth
    MousePosLabel=new QLabel;
    MousePosLabel->setText(tr(""));
    MousePosLabel->setFixedWidth(100);
    statusBar()->addPermanentWidget(statusLabel);//在状态栏中增加控件
    statusBar()->addPermanentWidget(MousePosLabel);//在状态栏中增加控件
    this->setMouseTracking(true);//设置窗体追踪鼠标
    resize(1366,768);//设置窗口大小
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{

 QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
if(e->button()==Qt::LeftButton)
{
    //左键按下   显示按下坐标
   statusBar()->showMessage(tr("左键:")+str);
}
if(e->button()==Qt::RightButton)
{
    //右键按下    显示按下坐标
    statusBar()->showMessage(tr("右键:")+str);
}
else if(e->button()==Qt::MidButton)
{
    //中键按下    显示按下坐标
    statusBar()->showMessage(tr("中键:")+str);
}

}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    //鼠标移动追踪事件   显示按下坐标
    MousePosLabel->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");

}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    //鼠标释放追钟事件
    QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
    statusBar()->showMessage(tr("释放在:")+str,3000);

}
//鼠标双击事件
void MainWindow::mouseDoubleClickEvent(QMouseEvent *e)
{
    QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
    statusBar()->showMessage(tr("双击在:")+str,3000);
}

main.cpp中配置

#include "mainwindow.h"
#include <QApplication>
//#include"mouseevent.h"

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

    return a.exec();
}

运行后结果

此例子我在一本QT5 开发及实例中看到的,陆文周主编,建议大家去看看。 

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,QLabel本身没有click信号可以响应点击事件。但是我们可以通过几种方法来实现点击事件的功能。 第一种方法是使用事件过滤器(event filter)。我们可以在QLabel上安装一个事件过滤器,然后在过滤器中判断是否发生了鼠标点击事件,并执行相应的操作。具体实现可以参考。 第二种方法是继承QLabel类并重写mousePressEvent函数。在自定义的myLabel类中,我们可以重新实现mousePressEvent函数以处理鼠标点击事件。具体实现可以参考。 第三种方法是继承QLabel类并重写mouseReleaseEvent函数。在自定义的my_Label类中,我们可以重新实现mouseReleaseEvent函数以处理鼠标左键点击事件,并在其中发射一个clicked信号。具体实现可以参考。 这些方法都可以实现在QLabel上添加点击事件的功能,具体选择哪种方法取决于你的需求和代码结构。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [QT学习笔记(十五):QLabel的点击事件(clicked)添加](https://blog.csdn.net/Vichael_Chan/article/details/100143032)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值