繁体   English   中英

在Qt中将组合框信号连接到std :: function时出现问题

[英]Problems connecting a combobox signal to a std::function in Qt

我正在创建一个QWizard。 对于某些QWizardPages,我在其构造函数中为其提供了回调方法,例如QWizardPage可以在QWizard上设置一些属性。

像这样:

BWizard::BWizard(QWidget *parent, t::WindowFlags ): QWizard(parent, f), modelCheckedOut(false), selectedB(0){
  // Setup UI and add pages to wizard.
  ui.setupUi(this);
  // Make callbacks to set data onto Wizard instead of pages.
  auto makeLastPage = [this]()
  {
    int pageId = this->pageIds().last();
    this->removePage(pageId);
    this->modelCheckedOut = true;
  };
  auto setB = [this](int b)
  {
    this->selectedB = b;
  };
  addPage(new BWizardPage1(setB, this));
  addPage(new BWizardPage2(makeLastPage, this));
  addPage(new BWizardPage3(this));
}

BWizardPage1当在QLineEdit上按Enter时,我使用setB函数。 我将此绑定到BWizardPage1的构造函数中。 这工作正常:

QObject::connect(ui.searchField, &QLineEdit::returnPressed, this, [&]() {
  setB(5);
}

BWizardPage2上,更改组合框时,我使用makeLastPage函数。 我将此绑定在BWizardPage2的构造函数中。 更改组合框后, makeLastPage似乎为空(或为null)。

 QObject::connect(ui.comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [&](int index) {
    makeLastPage();
}

谁能告诉我我在做什么错? 我将函数传递为std::function<void(void)> makeLastPagestd::function<void(int)> setB

QObject::connect(ui.comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [&](int index) {
    makeLastPage();
}

通过按引用捕获makeLastPage ,就可以捕获对函数参数的引用。 函数返回后,它将超出范围,留下一个悬空的引用。 只需按值捕获makeLastPage

QObject::connect(ui.comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [=](int index) {
    makeLastPage();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM