qt uses the scroll wheel to control the scrollArea with the mouse as the center zoom

1 Override the wheelEvent function of the QAbstractScrollArea class so that it does nothing

void QAbstractScrollArea::wheelEvent(QWheelEvent *event)
{
    
    
	return;
}

2 Realize the enlargement and reduction of Qlabel in QScrollArea

void OpenAndSplicingWindow::wheelEvent(QWheelEvent *event)
{
    
    
	ui->label->setMinimumSize(0, 0);
	ui->label->setMaximumSize(2048, 2048);
	QRect tmp = ui->label->geometry();

	int adjustSize = 20;//改变的尺寸

	//如果滚轮向上滑动,且鼠标位置在scrollArea里面
	if (event->delta() > 0 && tmp.contains(event->pos()))
	{
    
    
		tmp.setWidth(tmp.width() + adjustSize);
		tmp.setHeight(tmp.height() + adjustSize);
		ui->lb_Screen->setFixedSize(tmp.width(), tmp.height());
	}
	if (event->delta() < 0 && tmp.contains(event->pos()))
	{
    
    
		tmp.setWidth(tmp.width() - adjustSize);
		tmp.setHeight(tmp.height() - adjustSize);
		ui->lb_Screen->setFixedSize(tmp.width(), tmp.height());
	}

	//重新定位滚动条为鼠标位置
	QScrollBar *tmph = ui->scrollArea->horizontalScrollBar();
	QScrollBar *tmpv = ui->scrollArea->verticalScrollBar();
	QPoint pos = event->pos();
	tmph->setValue(pos.x());
	tmpv->setValue(pos.y());
}

Guess you like

Origin blog.csdn.net/Fengfgg/article/details/113395003