有没有办法在不同标题的函数之间指向变量?

时间:2019-04-04 02:50:17

标签: c++ function qt header

简而言之,我正在尝试为绘画应用程序的画布设置对话框编写代码。 我的第一个认识是,我在两个单独的标头中声明了这两个函数。我想知道有什么方法可以在createCanvas函数中创建一个指向createSpinBoxes函数的指针吗?

我的第一个认识是我在两个单独的头文件中声明了这两个函数。 我也尝试过使用指针,但是它们并没有真正起作用。 这两个函数也位于两个不同的类中。

//This one is from a header file called "canvassetupdialog.h"
void canvasSetupDialog::createSpinBoxes()
{
int def_canW = 1920;
int def_canH = 1080;

//For the canvas Width
QSpinBox *canvasWidthSpinBox = new QSpinBox;
canvasWidthSpinBox->setRange(1, 20000);
canvasWidthSpinBox->setSingleStep(1);
canvasWidthSpinBox->setValue(def_canW);

//For the canvas Height
QSpinBox *canvasHeightSpinBox = new QSpinBox;
canvasHeightSpinBox->setRange(1, 20000);
canvasHeightSpinBox->setSingleStep(1);
canvasHeightSpinBox->setValue(def_canH);
//I wanted to be able to use these pointers in the other function below.
int *canWptr = &def_canW;
int *canHptr = &def_canH;
}

//This one is from a header file called "scribblearea.h"

void ScribbleArea::createCanvas(QImage *canvas)
{
canvas->width() = *canWptr;
canvas->height() = *canHptr;
}

我希望得到的结果是,在旋转框中选择的任何值都是用户要绘制的画布的设置宽度和高度。 (我的第二个猜测是,我应该坚持将这两个函数保留在一个头文件中)

1 个答案:

答案 0 :(得分:0)

更改ScribbleArea::createCanvas()以将宽度和高度作为参数:

void ScribbleArea::createCanvas(QImage *canvas, int width, int height)
{
    *canvas = canvas->scaled(width, height);
}

虽然不确定您要在这里做什么,但是

同样,您的原始代码:

canvas->width() = width;

不执行任何操作。 canvas->width()返回一个int值,您正在尝试为其分配值。那不行我假设您想更改图像的大小。为此,您需要使用QImage::scaled()创建该图像的缩放副本并将该新图像分配回原始图像。 (您不能调整QImage的大小。只能为其按比例缩放副本。)