Qt鼠标事件总结

//鼠标按下事件
void QWidget::mousePressEvent(QMouseEvent *event)
{
    QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
  //  ui->lineEdit->setText(tr("鼠标按下")+str);
    if(event->button()==Qt::LeftButton)
    {
        ui->lineEdit->setText(tr("鼠标左键按下:")+str);
    }
    else if(event->button()==Qt::RightButton)
    {
         ui->lineEdit->setText(tr("鼠标右键按下:")+str);
    }
    else if(event->button()==Qt::MidButton)
    {
         ui->lineEdit->setText(tr("鼠标中键按下:")+str);
    }
}
//鼠标移动事件
void QWidget::mouseMoveEvent(QMouseEvent *event)
{
    QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
   // ui->lineEdit->setText(tr("鼠标移动")+str);
    if(event->buttons()&Qt::LeftButton)
        ui->lineEdit->setText(tr("鼠标左键按下移动")+str);
    else if(event->buttons()&Qt::RightButton)
        ui->lineEdit->setText(tr("鼠标右键按下移动")+str);
    else if(event->buttons()&Qt::MidButton)
        ui->lineEdit->setText(tr("鼠标中键按下移动")+str);
}
//鼠标释放事件
void QWidget::mouseReleaseEvent(QMouseEvent *event)
{
    QString str="("+QString::number(event->x())+","+QString::number(event->y())+")";
  //  ui->lineEdit->setText(tr("鼠标释放")+str);
    if(event->button()==Qt::LeftButton)
    {
        ui->lineEdit->setText(tr("鼠标左键释放:")+str);
    }
    else if(event->button()==Qt::RightButton)
    {
         ui->lineEdit->setText(tr("鼠标右键释放:")+str);
    }
    else if(event->button()==Qt::MidButton)
    {
         ui->lineEdit->setText(tr("鼠标中键释放:")+str);
    }
}

注意移动时判断鼠标状态的方法与按下和释放时判断方法不同

你可能感兴趣的:(qt)