简体   繁体   English

QGraphicsView用qpushbutton放大和缩小

[英]QGraphicsView Zooming in and out with qpushbutton

I have a QGraphicsScene and i want to do zooming ( in and out ) with a qpushbutton , i have this code : 我有一个QGraphicsScene ,我想用qpushbutton进行缩放(放大和缩小),我有以下代码:

void fonction(){
        Scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(Scene);
        QPen Pen ;
        QBrush Brush(Qt::red) ;
        Pen.setWidth(5);
        ellipse = Scene->addEllipse(2,2,30,30,Pen,Brush);
}

How to do it ? 怎么做 ? I have tried : 我努力了 :

void HomePage::on_pushButton_4_clicked()
{
    if(Zoom!=10 ){
        Zoom=Zoom+0.1 ;
       }
    ui->graphicsView->show();

}

But something does not happen . 但是什么都不会发生。

You haven't used your Zoom variable to do anything with QGraphicsView. 您尚未使用Zoom变量对QGraphicsView进行任何操作。 You'll need to call: 您需要致电:

ui->graphicsView->scale (Zoom, Zoom);

This will change the zoom level. 这将更改缩放级别。 The method scales the current transformation by the value of Zoom. 该方法通过Zoom的值缩放当前变换。 Your code doesn't show what Zoom starts as, but you'll want it to start 1, I suspect. 您的代码没有显示Zoom的开头,但是我怀疑您希望它以1开始。 If it starts at 0, so that the first Zoom level is 0.1, then your scene will zoom very far out on the first button press. 如果从0开始,则第一个“缩放”级别为0.1,则您的场景将在第一次按下按钮时放大到很远。

In order to control this, you may want to use ui->graphicsView->transform() to get the current transformation matrix and look at the m11 and m22 values for the current zoom levels. 为了控制它,您可能需要使用ui-> graphicsView-> transform()来获取当前的变换矩阵,并查看当前缩放级别的m11和m22值。

Again, the scale method scales the current transformation , so each time you call it, you're scaling from the current scale factors, not the original values (which are 1 by default, indicating no scaling transformation). 同样,scale方法会缩放当前转换 ,因此,每次调用它时,都是从当前缩放比例缩放,而不是从原始值缩放(默认值为1,表示没有缩放转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM