您当前的位置: 首页 >  qt

txwtech

暂无认证

  • 3浏览

    0关注

    813博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

QT鼠标事件与滚轮事件-拖动窗口-双击全屏-右键显示自定义鼠标图标

txwtech 发布时间:2022-09-10 22:33:15 ,浏览量:3

QT鼠标事件与滚轮事件-拖动窗口-双击全屏-右键显示自定义鼠标图标
void MainWindow6_3::mouseMoveEvent(QMouseEvent *event)实现拖动窗口移动
滚轮事件时间编辑框文字放大缩小功能

#ifndef MAINWINDOW6_3_H
#define MAINWINDOW6_3_H
//by txwtech
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow6_3; }
QT_END_NAMESPACE

class MainWindow6_3 : public QWidget
{
    Q_OBJECT

public:
    MainWindow6_3(QWidget *parent = nullptr);
    ~MainWindow6_3();

private:
    Ui::MainWindow6_3 *ui;
    QPoint offset2;//用来储存鼠标指针位置与窗口位置的差值
protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);

};
#endif // MAINWINDOW6_3_H

 

#include "mainwindow6_3.h"
#include "ui_mainwindow6_3.h"
#include  //

MainWindow6_3::MainWindow6_3(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWindow6_3)
{
    ui->setupUi(this);
    //
    QCursor cursor2;//创建光标对象
    cursor2.setShape(Qt::OpenHandCursor);//设置光标的形状
  //  cursor2.setShape(Qt::CrossCursor);//设置光标的形状
    setCursor(cursor2); //使用光标
   // setMouseTracking(true);
}

MainWindow6_3::~MainWindow6_3()
{
    delete ui;
}

void MainWindow6_3::mousePressEvent(QMouseEvent *event)
{
   if(event->button()==Qt::LeftButton)
   {
       //左键按下
       QCursor cursor2a;
       cursor2a.setShape(Qt::ClosedHandCursor);
       QApplication::setOverrideCursor(cursor2a);//是鼠标指针暂时改变形状
       offset2=event->globalPos()-pos();//获取指针位置和窗口位置的差值
   }
   else if(event->button()==Qt::RightButton)//by txwtech
   {
       //右键按下
       QCursor cursor2ar(QPixmap("../src6_3/logo_txwtech.png"));
      // QCursor cursor2ar(QPixmap("logo_txwtech.png"));
       QApplication::setOverrideCursor(cursor2ar);//设置自定义图片作为鼠标指针
   }
}

void MainWindow6_3::mouseReleaseEvent(QMouseEvent *event)
{
   Q_UNUSED(event);//event参数没有用的话,编译会报警,加上词句就可以忽略,编译器不报警
    QApplication::restoreOverrideCursor();
}

void MainWindow6_3::mouseDoubleClickEvent(QMouseEvent *event)
{
    //双击事件
    if(event->button()==Qt::LeftButton)
    {
        if(windowState()!=Qt::WindowFullScreen)
        {
            setWindowState(Qt::WindowFullScreen);//设置全屏
        }
        else
        {
            setWindowState(Qt::WindowNoState);//恢复以前的大小
        }
    }
}

void MainWindow6_3::mouseMoveEvent(QMouseEvent *event)
{
    //鼠标移动事件
    if(event->buttons() & Qt::LeftButton)//这里必须使用buttons(),这里多了一个s,注意与mousePressEvent里面判断左键的区分
    {
        QPoint temp;
        temp=event->globalPos()-offset2;
        move(temp);//使用鼠标指针当前的位置减去差值,就得到了窗口应该移动的位置
    }

}

void MainWindow6_3::wheelEvent(QWheelEvent *event)
{
   if(event->delta()>0)
   {
       ui->textEdit->zoomIn();//拉近,放大
   }
   else
   {
       ui->textEdit->zoomOut();//缩小
   }
}

关注
打赏
1665060526
查看更多评论
立即登录/注册

微信扫码登录

0.0388s