QT鼠标事件重写小案例

//注意事件头文件
//图片事件重写
void Widget::paintEvent(QPaintEvent *event)
{
     
    QPainter p(this);
    p.drawPixmap(0,0,QPixmap("../Image/3.jpg"));


}
//鼠标点击事件重写
void Widget::mousePressEvent(QMouseEvent *event)
{
     
    if(event->button() == Qt::RightButton)
    {
     
        close();
    }
    else if(event->button() == Qt::LeftButton)
    {
     
        //求差值
        p = event->globalPos() - this->frameGeometry().topLeft();
    }
}
//鼠标移动事件重写
void Widget::mouseMoveEvent(QMouseEvent *event)
{
     
if(event->buttons() &Qt::LeftButton)
{
     
    move(event->globalPos()-p);
}
}

你可能感兴趣的:(Qt,qt)