如何在Qt中绘制缩放图像

时间:2019-02-04 17:15:36

标签: c++ image qt user-interface zooming

我正在一个项目中,我必须做一个类似于Paint的图像编辑器。我想打开图像,必要时放大/缩小,然后在其上绘制。

为此,我使用了Image Viewer和Scribble示例,我添加的唯一不同之处是QLabel子类,该子类应该使用鼠标按下/鼠标释放事件来绘制线条和其他形式。问题在于重写的paintEvent。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void explain();
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A);

int main()
{
    double a, b, c;
    double xi, xf, xc, x = 0;
    double fx = 0, fD = 0, A = 0;

    printf("SECOND DEGREE POLYNOMIAL CALCULATOR\n\n");
    explain();

    printf("\n\nEnter a value for a: ");
    scanf("%lg", &a);
    printf("Enter a value for b: ");
    scanf("%lg", &b);
    printf("Enter a value for c: ");
    scanf("%lg", &c);

    printf("\nYour function is %lgx^2%+-lgx%+-lg", a, b, c);

    printf("\n\nEnter your initial x-value: ");
    scanf("%lg", &xi);
    printf("Enter your final x-value: ");
    scanf("%lg", &xf);
    printf("Enter what you would like to increment by: ");
    scanf("%lg", &xc);

    printf("|   x   |   f(x)   |   f'(x)   |   A   |\n"); //printing table
    printf("----------------------------------------\n");

    x = xi;
    double nextX;
    while (x <= xf) {
        nextX = x + xc;
        math(a, b, c, x, nextX, &fx, &fD, &A);
        printf("| %.3lf | %.3lf | %.3lf | %.3lf |\n", x, fx, fD, A);
        x = x + xc;
    }

    return 0;
}
void explain() {
    printf("This program computes the integral and derivative of a user inputted second-degree polynomial (f(x)=ax^2+bx+c).\n");
    printf("You will be asked to enter the 3 coefficients of your polynomial, followed by your initial x-value, your\n");
    printf("final x-value, and the increment value between each x.");
}
void math(double a, double b, double c, double x, double xi, double *fx, double *fD, double *A) {
    *fx = (a*(x*x)) + (b*x) + c;  //finding y values
    *fD = (2 * a * x) + b;   //finding derivative values
    *A = ((a / 3)*pow(x, 3) + (a / 2)*pow(x, 2) + c * x) - ((a / 3)*pow(xi, 3) + (a / 2)*pow(xi, 2) + c * xi);  //finding integral values

    return;
}

我一开始可以正常放大,但是当我开始绘制图像时,图像会恢复到原始大小,而未被图像占据的其余标签将完全变为白色,因为它保持了缩放大小。

是否有一种方法可以像画图一样在放大时保持图像放大?

更新

这是draw函数的代码,也许会帮忙

void imLabel::paintEvent(QPaintEvent *event){
   if(tipo != ""){ //draw mode
      QPainter painter(this);
      QRect dirtyRect = event->rect();
      painter.drawImage(dirtyRect,image,dirtyRect);
   }
   else QLabel::paintEvent(event);
}

0 个答案:

没有答案