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

1

I have a QComboBox which should be updated on CurrentTextChanged. I have created the following connect statement so that the QComboBox fires the onFilterComboBoxCurrentTextChanged slot:

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

clearAndAddDICOMTagsToShownTagsListWidget causes the following errors. How can I solve this?

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).

enter image description here

c++
qt
exception
stack-overflow
asked on Stack Overflow Jan 4, 2016 by Engo • edited Jan 4, 2016 by Engo

2 Answers

2

The recursion looks like this:

  1. Text changes in combobox
  2. You call clear (it causes text change)
  3. Go back to 1 - recursion ready.

If you need to stop signal propagation when you perform your operation call blockSignals on your combobox object. However, I would really rethink your app logic. I work with Qt a lot, and didn't need to use blockSignals a lot.

answered on Stack Overflow Jan 4, 2016 by Kamil Klimek • edited Sep 3, 2016 by Krish Munot
1

The easy fix is to block signals before updating combo box's items. Something like this :

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

But I believe you should be able to prevent it by a better design of your code.

answered on Stack Overflow Jan 4, 2016 by Mehrdad Momeny

User contributions licensed under CC BY-SA 3.0