Home > Net >  Qt C - How to pass slider value into function that will be slotted into another slider?
Qt C - How to pass slider value into function that will be slotted into another slider?

Time:10-16

I'm super new to Qt so bear with me.

I'm doing my class assignment in which I need to create a window with 3 sliders and a label in which I can load my image. These 3 sliders are corresponding to HSL (Hue, Saturation, Luminance) that of course will change my image.

Math needed for these transformations is done. I have problem strictly to Qt, because I've never worked with it previously.

Assuming I have function in which I'm doing all transformations:

void hsl(const QImage& src, QImage& dst, float delta_h, float delta_s, float delta_l)

Then, I have function to apply these transformation to my image:

void MainWindow::changeHSL(int h, int s, int l) {
    hsl(originalImage, processImage, h, s, l);
    ui->label->setPixmap(QPixmap::fromImage(processImage));
}

And then, I need to slot this function to my sliders. And this is where I have problem.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(openImage(bool)));
    connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(changeHSL(int, int, int)));
    connect(ui->horizontalSlider_2, SIGNAL(valueChanged(int)), this, SLOT(changeHSL(int, int, int)));
    connect(ui->horizontalSlider_3, SIGNAL(valueChanged(int)), this, SLOT(changeHSL(int, int, int)));
}

Slotting this function that way is incorrect, I have errors with incompatible sender/receiver arguments. Changing function to have one argument is also prohibited, I need it to have three arguments like how I wrote it. I asked my teacher during class how I should resolve this, but he said that in every sliders somehow I need to pass value from other sliders. Unfortunately, my experience with Qt is none and I simply don't have any idea how to do it.

If you're afraid to help me because of "class assignment" don't worry - it is only about math behind image processing. Qt is only a tool chosen by my teacher because most of my colleagues had experience with it. Unfortunately I do not, so I'm trying to catch up real quick. Thanks for all replies.

CodePudding user response:

As it is a homework, just a hint: you do not need the value passed by the signal, you can read the current state of each slider with QAbstractSlider::value(): https://doc.qt.io/qt-6/qabstractslider.html#value-prop.

CodePudding user response:

You can't connect signals and slots like that. Try use my solution:

Remove the parameters of MainWindow::changeHSL(int h, int s, int l), replace the h, s, l into the values of h, s, l sliders (They're ui->horizontalSlider, ui->horizontalSlider_2, ui->horizontalSlider_3), like this:

void MainWindow::changeHSL() {
    int h = ui->horizontalSlider->value();
    int s = ui->horizontalSlider_2->value();
    int l = ui->horizontalSlider_3->value();

    hsl(originalImage, processImage, h, s, l);
    ui->label->setPixmap(QPixmap::fromImage(processImage));
}

Rewrite the connections:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(openImage(bool)));
    connect(ui->horizontalSlider, &QSlider::valueChanged, this, &MainWindow::changeHSL);
    connect(ui->horizontalSlider_2, &QSlider::valueChanged, this, &MainWindow::changeHSL);
    connect(ui->horizontalSlider_3, &QSlider::valueChanged, this, &MainWindow::changeHSL);
}

Hard to explain why didn't your code go wrong because I had a bad English. But anyway, I hope my code will help you something ;)

  •  Tags:  
  • c qt
  • Related