qt 鼠标事件

qt 鼠标事件_第1张图片

void Widget::mousePressEvent(QMouseEvent *event)  //左键变形状
{
    if(event->button()==Qt::LeftButton){
        QCursor cursor;
        cursor.setShape(Qt::ClosedHandCursor);
        QApplication::setOverrideCursor(cursor);
        offset=event->globalPos()-pos();
    }
}
void Widget::mouseReleaseEvent(QMouseEvent *event){  //释放鼠标 
    Q_UNUSED(event);
    QApplication::restoreOverrideCursor();
}

声明如下 .h

#ifndef WIDGET_H
#define WIDGET_H

#include 
class MylineEdit;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    bool eventFilter(QObject *obj, QEvent *event);
    ~Widget();

private:
    Ui::Widget *ui;
    MylineEdit *lineEdit;
    QPoint offset;
protected:
    void keyPressEvent(QKeyEvent *event);\
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
};

#endif // WIDGET_H

qt 鼠标事件_第2张图片

你可能感兴趣的:(QT,qt,ui,开发语言)