0

In short I'm trying code a canvas setup dialog for a paint application. My first realization was that I declared these two functions in two separate headers. I was wondering is there any way I can make a pointer in the createCanvas function that points to the createSpinBoxes function?

My first realization was that I declared these two functions in two separate headers. I've also tried using pointers, but they're not really working. These two functions are also in two different classes.

//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;
}

What I wanted this to result in is that whatever value is picked in the spinboxes will be the set width and height of the canvas the user is going to draw on. (My second guess is that I should stick to keeping these two functions in one header file)

  • I think you want to use extern: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – Amadeus Apr 04 '19 at 02:55

1 Answers1

0

Change ScribbleArea::createCanvas() to take the width and height as parameters:

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

Not sure what you're trying to do here exactly though.

Also, your original code:

canvas->width() = width;

does not do anything. canvas->width() returns an int value and you're trying to assign to it. That doesn't work. I assume you want to change the size of the image. To do that, you need to use QImage::scaled() to create a scaled copy of the image and assign that new image back to the original. (You can not resize a QImage. You can only create scaled copies of it.)

Nikos C.
  • 50,738
  • 9
  • 71
  • 96