是什么导致我在 Qt 中的第一次机会(堆栈溢出)异常?

What is causing my first-chance (stack overflow) exception in Qt?

我有一个 QComboBox,它应该在 CurrentTextChanged 上更新。我创建了以下连接语句,以便 QComboBox 触发 onFilterComboBoxCurrentTextChanged 插槽:

connect(m_viewController->getFilterWindow()->getFilterComboBox(), &QComboBox::currentTextChanged, this, &MainController::onFilterComboBoxCurrentTextChanged);

clearAndAddDICOMTagsToShownTagsListWidget 导致以下错误。我该如何解决?

void MainController::onFilterComboBoxCurrentTextChanged(QString filterName)
{
    m_viewController->clearAndAddFilterNamesToFilterComboBox(m_fileController->loadFilterNamesFromConfigFile());
}

QStringList FileController::loadFilterNamesFromConfigFile()
{
    QSettings settings(QDir::toNativeSeparators("C:\HelloWorld\Config\Filter.cfg"), QSettings::IniFormat);
    QStringList filtersNames = settings.childGroups();
    return filtersNames;
}

    void ViewController::clearAndAddFilterNamesToFilterComboBox(QStringList filterNames)
{
    m_filterWindow.getFilterComboBox()->clear();
    m_filterWindow.getFilterComboBox()->addItems(filterNames);
}

QListWidget* FilterWindow::getShownTagsListWidget()
{
    return ui.shownTagsListWidget;
}

First-chance exception at 0x777EAFC0 (ntdll.dll) in DoseView.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002C2FFC).

Unhandled exception at 0x777EAFC0 (ntdll.dll) in DoseView.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x002C2FFC).

递归看起来像这样:

  1. 组合框中的文本更改
  2. 你调用clear(它会导致文本改变)
  3. 返回 1 - 递归就绪。

如果您在对组合框对象执行操作时需要停止信号传播,请调用 blockSignals。但是,我真的会重新考虑您的应用程序逻辑。我经常使用 Qt,不需要经常使用 blockSignals。

简单的解决方法是在更新组合框的项目之前阻止信号。像这样:

void MainController::onFilterComboBoxCurrentTextChanged(QString filterName) {
combobox->blockSignals(true); 
m_viewController->clearAndAddFilterNamesToFilterComboBox(m_fileController->loadFilterNamesFromConfigFile());
combobox->blockSignals(false);
}

但我相信您应该能够通过更好地设计代码来防止它发生。