【问题标题】:How to use a variable from a slot in another slot in QT如何在 QT 中的另一个插槽中使用插槽中的变量
【发布时间】:2016-07-15 15:25:21
【问题描述】:

我正在学习 QT,遇到了这个问题。我在这里阅读了一些几乎类似的问题,但是对于像我这样的新手来说,解决方案很复杂。

如何在这段代码中访问var_slot1 的值?

请帮我解决这个问题。非常感谢!

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

【问题讨论】:

  • 使var_slot1成为MyProject的成员变量
  • 非常感谢,史蒂夫! :)

标签: c++ qt signals-slots


【解决方案1】:

您可以将值保存为类成员变量而不是函数局部变量。然后你就可以从两个成员函数中访问它了。

【讨论】:

  • 不要忘记在构造函数中初始化它,以防在 slot1() 之前调用 slot2()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
相关资源
最近更新 更多