qt 鼠标点击事件
大概就这几种,
按左键右键
void QtWidgetsApplication7::mousePressEvent(QMouseEvent *event)
{
//如果是鼠标左键按下
if (event->button() == Qt::LeftButton) {
QCursor cursor;
cursor.setShape(Qt::ClosedHandCursor);
QApplication::setOverrideCursor(cursor);
offset = event->globalPos() - pos();
}
else if (event->button() == Qt::RightButton) {
QCursor cursor(QPixmap(":/res/mouse.png"));
QApplication::setOverrideCursor(cursor);
}
}
释放事件
void QtWidgetsApplication7::mouseReleaseEvent(QMouseEvent *event)
{
//释放事件
QApplication::restoreOverrideCursor();
}
双击事件
void QtWidgetsApplication7::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (windowState() != Qt::WindowFullScreen) {
setWindowState(Qt::WindowFullScreen);
}
else {
setWindowState(Qt::WindowNoState);
}
}
}
鼠标移动事件
void QtWidgetsApplication7::mouseMoveEvent(QMouseEvent *event)
{
//移动过程中判断鼠标是左键点击并且移动,那么要用buttons,返回的是鼠标状态的集合
if (event->buttons() & Qt::LeftButton) {
//获取窗口应当移动到的位置
QPoint windowpos = event->globalPos() - offset;
this->move(windowpos);
}
}
滚轮事件
void QtWidgetsApplication7::wheelEvent(QWheelEvent *event)
{
//鼠标滚动远离使用者放大textedit
if (event->delta() > 0) {
qDebug() << "catch wheel event delta > 0" << endl;
ui.textEdit->zoomIn();
}
else {
qDebug() << "catch wheel event delta < 0" << endl;
ui.textEdit->zoomOut();
}
}