@Christian-Ehrlicher Thanks for your quick reply. I think I found the solution. With the updated syntax below the code calls the lambda slot in the MainWindow's constructor connect and it also calls the connection slot the dialog when if it is opened while the audio inputs or outputs are changed (by unplugging a headset for example).
Up to this point, I was having a qobject_cast errors, as I was including the wrong header and incorrectly doing forward declarations. (parent below represents my mainwindow). The following code in the constructor of the SettingsDialog child class works well and more to the point, I no longer need to have duplicated objects (one in the MainWindow and a separate one in the SettingsDialog.
// hot plugging audio support through parent field
auto* mediaDevices = qobject_cast<MainWindow*>(parent)->mMediaDevices.get();
// audio input change handler - supports hot plug.
// context parameter ensures auto disconnect.
connect (mediaDevices,&QMediaDevices::audioInputsChanged, this,
[this, &rSettings] {
auto* pComboBox = ui->inputDevices;
pComboBox->clear();
for (const auto& next : QMediaDevices::audioInputs()) {
if (!next.isNull()) {
pComboBox->addItem(next.description(),
QVariant::fromValue(next));
}
}
// add generator device
static auto gDevice = gGeneratorInfo->create();
pComboBox->addItem(gDevice.description(),
QVariant::fromValue(gDevice));
const auto settingsDeviceName =
rSettings.audioInput.description();
(void)settingsDeviceName;
for (auto index = 0; index < pComboBox->count(); ++index) {
// search for matching audio device in the settings
if (pComboBox->itemData(index).isValid() &&
pComboBox->itemData(index).value<
QAudioDevice>() == rSettings.audioInput) {
pComboBox->setCurrentIndex(index);
return;
}
}
// device specified in settings unplugged - replace with default device
pComboBox->setCurrentIndex(
pComboBox->findData(QVariant::fromValue(
QMediaDevices::defaultAudioInput())));
});
// audio output change handler - supports hot plug.
// context parameter ensures auto disconnect.
connect(mediaDevices, &QMediaDevices::audioOutputsChanged, this,
[this, &rSettings] {
auto* pComboBox = ui->outputDevices;
pComboBox->clear();
for (const auto& next : QMediaDevices::audioOutputs()) {
if (!next.isNull()) {
pComboBox->addItem(next.description(),
QVariant::fromValue(next));
}
}
// add rtp device
static auto gDevice = gRTPDeviceInfo->create();
pComboBox->addItem(gDevice.description(),
QVariant::fromValue(gDevice));
const auto settingsDeviceName =
rSettings.audioOutput.description();
(void)settingsDeviceName;
for (auto index = 0; index < pComboBox->count(); ++index) {
// search for matching audio device in the settings
if (pComboBox->itemData(index).isValid() &&
pComboBox->itemData(index).value<
QAudioDevice>() == rSettings.audioOutput) {
pComboBox->setCurrentIndex(index);
return;
}
}
// device specified in settings unplugged - replace with default device
pComboBox->setCurrentIndex(
pComboBox->findData(QVariant::fromValue(
QMediaDevices::defaultAudioOutput())));
});