[Qt] 事件机制(四)
滚轮事件:滚动滚轮实现窗口大小缩放
widget.h中增加:
protected: void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE; void extendWindowsSize(); void shrinkWindowsSize(); private: int m_originalWidth; int m_originalHeight;
widget.cpp中增加:
1 Widget::Widget(QWidget *parent) : 2 QWidget(parent), 3 ui(new Ui::Widget) 4 { 5 ... 6 m_originalWidth=this->width(); 7 m_originalHeight=this->height(); 8 } 9 void Widget::wheelEvent(QWheelEvent *event){ 10 QWidget::wheelEvent(event); 11 QPoint scroll = event->angleDelta(); 12 if(scroll.y()>0){ 13 extendWindowsSize(); 14 }else{ 15 shrinkWindowsSize(); 16 } 17 } 18 void Widget::extendWindowsSize(){ 19 if(this->width()>m_originalWidth+300){ 20 return; 21 } 22 this->setMaximumSize(this->width()+10,this->height()+10); 23 this->setMinimumSize(this->width()+10,this->height()+10); 24 } 25 void Widget::shrinkWindowsSize(){ 26 if(this->width()<m_originalWidth-300){ 27 return; 28 } 29 this->setMinimumSize(this->width()-10,this->height()-10); 30 this->setMaximumSize(this->width()-10,this->height()-10); 31 }
参考:
https://blog.csdn.net/qq78442761/article/details/85104679
https://blog.csdn.net/qq78442761/article/details/81503006
void setResizeAnchor(ViewportAnchor anchor);
QGraphicsView::AnchorViewCenter