Gansroy
Gansroy

Reputation: 229

How to get integer value from QSpinBox changed value?

Here, I am just trying to obtain the changed int value from QSpinBox by using SIGNAL and SLOTS because I need to use this int variable in my another function. I mean if the user changes QSpinBox values, it changes the value of int variable.

I know this SIGNAL will be SIGNAL(valueChanged(int)), but I got confused about what the SLOT will be in this case. I really got stack on this point.

EDIT:

What I have tried is following. MainWindow.h is

#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT

public slots:
    void setFrame(int frame);

MainWindow.cpp is following. To check how it is working I tried to display the int in a QLabel.

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(setFrame(int frame)));
}
void MainWindow::setFrame(int frame)
{
    ui->label->setText(QString::number(frame));

}

Can anyone fix this regarding how to get the changed int value from QSpinBox?

Thanks in advance.

Upvotes: 0

Views: 3444

Answers (1)

Lidia Parrilla
Lidia Parrilla

Reputation: 599

As the documentation states (in this document about QObject Class):

Note that the signal and slots parameters must not contain any variable names

So your mistake is specifying the variable name in the slot SLOT(setFrame(int frame)). As you must only use the variable type, it would be instead SLOT(setFrame(int)).

Anyway, if that's the only objective of the connect, I would rather do:

connect(ui->spinBox, SIGNAL(valueChanged(int)), ui->label, SLOT(setNum(int)));

That way, you don't even need the method setFrame in your MainWindow and labels already have a way to show numbers instead of strings.

Hope this helps!

Upvotes: 1

Related Questions