gnase
gnase

Reputation: 638

How to use a variable from a slot in another slot in QT

I am learning QT and have this problem. I have read some nearly similar questions here but the solutions are complicated to a newbie like me.

How can I access the value of var_slot1 in this code?

Please help me to solve this. Many thanks !

MyProject::MyProject(QWidget *parent)
      : QWidget(parent)
{
    ui.setupUi(this);
    ..........
    ..........
    connect(ui.button1, SIGNAL(signal1()), this, SLOT(slot1()));
    connect(ui.button2, SIGNAL(signal2()), this, SLOT(slot2()));
}

void MyProject::slot1()
{
    int var_slot1 = 8;
}

void MyProject::slot2()
{
    int var_slot2 = var_slot1 + 2; 
}

Upvotes: 0

Views: 116

Answers (1)

Jesper Juhl
Jesper Juhl

Reputation: 31447

You can save the value as a class member variable rather than as a function-local variable. Then you'll be able to access it from both member functions.

Upvotes: 4

Related Questions