From 2a4489d060236f19375407d575600eef81a8ada9 Mon Sep 17 00:00:00 2001 From: Daniel Kamil Kozar Date: Thu, 17 Sep 2020 01:36:56 +0200 Subject: [PATCH] Fix crashes when removing audio/subtitle tracks from the main window (#340) Indices in both combo boxes must be updated, since an audio track index might have changed when a subtitle track is removed and vice versa : there is only one array which holds information about tracks that these combo boxes reference via the data contained in their items' UserRoles. --- tsMuxerGUI/tsmuxerwindow.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/tsMuxerGUI/tsmuxerwindow.cpp b/tsMuxerGUI/tsmuxerwindow.cpp index 0081a33..f4166a5 100644 --- a/tsMuxerGUI/tsmuxerwindow.cpp +++ b/tsMuxerGUI/tsmuxerwindow.cpp @@ -1,6 +1,7 @@ #include "tsmuxerwindow.h" #include +#include #include #include #include @@ -722,13 +723,19 @@ void TsMuxerWindow::removeTrackFromDefaultComboBox(QComboBox *targetComboBox, QC { targetCheckBox->setChecked(false); } - for (int i = comboBoxIdx + 1; i < targetComboBox->count(); ++i) - { - auto curTrackIdx = targetComboBox->itemData(i).toInt(); - targetComboBox->setItemData(i, curTrackIdx - 1); - } targetComboBox->removeItem(comboBoxIdx); - updateTracksComboBox(targetComboBox); +} + +static void fixupIndices(QComboBox *comboBox, int removedTrackIdx) +{ + for (int i = 0; i < comboBox->count(); ++i) + { + auto trackIdx = comboBox->itemData(i).toInt(); + if (trackIdx > removedTrackIdx) + { + comboBox->setItemData(i, trackIdx - 1); + } + } } void TsMuxerWindow::removeTrackFromDefaultComboBox(int trackRowIdx) @@ -745,6 +752,10 @@ void TsMuxerWindow::removeTrackFromDefaultComboBox(int trackRowIdx) removeTrackFromDefaultComboBox(ui->defaultSubTrackComboBox, ui->defaultSubTrackCheckBox, comboBoxIdx, trackRowIdx); } + fixupIndices(ui->defaultAudioTrackComboBox, trackRowIdx); + fixupIndices(ui->defaultSubTrackComboBox, trackRowIdx); + updateTracksComboBox(ui->defaultAudioTrackComboBox); + updateTracksComboBox(ui->defaultSubTrackComboBox); } void TsMuxerWindow::updateTracksComboBox(QComboBox *comboBox) @@ -757,11 +768,8 @@ void TsMuxerWindow::updateTracksComboBox(QComboBox *comboBox) } } -#include - void TsMuxerWindow::moveTrackInDefaultComboBox(int oldTrackRowIdx, int newTrackRowIdx) { - qDebug() << oldTrackRowIdx << newTrackRowIdx; auto currentSubTrack = ui->defaultSubTrackComboBox->currentData(); auto currentAudioTrack = ui->defaultAudioTrackComboBox->currentData(); ui->defaultSubTrackComboBox->clear(); @@ -2163,6 +2171,7 @@ void TsMuxerWindow::delTracksByFileName(const QString &fileName) void TsMuxerWindow::deleteTrack(int idx) { disableUpdatesCnt++; + removeTrackFromDefaultComboBox(idx); delete getCodecInfo(idx); int lastItemIndex = idx; // trackLV.items[idx].index; ui->trackLV->removeRow(idx); @@ -2192,7 +2201,6 @@ void TsMuxerWindow::deleteTrack(int idx) ui->removeTrackBtn->setEnabled(ui->trackLV->currentItem() != 0); disableUpdatesCnt--; trackLVItemSelectionChanged(); - removeTrackFromDefaultComboBox(idx); } void TsMuxerWindow::updateNum()