Hello,

i found a crash when one of the buttons in the TOC dialog is pressed if the TOC is empty. The attached patch disables the buttons if there are no entries in the TOC dialog. To be safe it also adds a test to the button handler functions whether the list has entries before accessing the one with index 0.

Bernhard
Index: src/frontends/qt4/QTocDialog.C
===================================================================
--- src/frontends/qt4/QTocDialog.C      (revision 17395)
+++ src/frontends/qt4/QTocDialog.C      (working copy)
@@ -140,9 +140,12 @@
 void QTocDialog::on_moveUpPB_clicked()
 {
        enableButtons(false);
-       QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
-       form_->goTo(index);
-       form_->outlineUp();
+       QModelIndexList &list = tocTV->selectionModel()->selectedIndexes();
+       if (!list.isEmpty()) {
+               QModelIndex index = list[0];
+               form_->goTo(index);
+               form_->outlineUp();
+       }
        update();
 }
 
@@ -150,9 +153,12 @@
 void QTocDialog::on_moveDownPB_clicked()
 {
        enableButtons(false);
-       QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
-       form_->goTo(index);
-       form_->outlineDown();
+       QModelIndexList &list = tocTV->selectionModel()->selectedIndexes();
+       if (!list.isEmpty()) {
+               QModelIndex index = list[0];
+               form_->goTo(index);
+               form_->outlineDown();
+       }
        update();
 }
 
@@ -160,9 +166,12 @@
 void QTocDialog::on_moveInPB_clicked()
 {
        enableButtons(false);
-       QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
-       form_->goTo(index);
-       form_->outlineIn();
+       QModelIndexList &list = tocTV->selectionModel()->selectedIndexes();
+       if (!list.isEmpty()) {
+               QModelIndex index = list[0];
+               form_->goTo(index);
+               form_->outlineIn();
+       }
        update();
 }
 
@@ -170,9 +179,12 @@
 void QTocDialog::on_moveOutPB_clicked()
 {
        enableButtons(false);
-       QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
-       form_->goTo(index);
-       form_->outlineOut();
+       QModelIndexList &list = tocTV->selectionModel()->selectedIndexes();
+       if (!list.isEmpty()) {
+               QModelIndex index = list[0];
+               form_->goTo(index);
+               form_->outlineOut();
+       }
        update();
 }
 
@@ -217,7 +229,7 @@
 {
        QStringListModel * type_model = form_->typeModel();
        if (type_model->stringList().isEmpty()) {
-               enableButtons();
+               enableButtons(false);
                typeCO->setModel(type_model);
                tocTV->setModel(new QStandardItemModel);
                tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
@@ -228,19 +240,21 @@
        typeCO->setModel(type_model);
        typeCO->setCurrentIndex(form_->getType());
 
+       bool buttonsEnabled = false;
        if (form_->tocModel()) {
+               buttonsEnabled = form_->tocModel()->rowCount() > 0;
                tocTV->setModel(form_->tocModel());
                tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
        }
        // avoid flickering
-       tocTV-> setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+       tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        tocTV->showColumn(0);
        // hide the pointless QHeader for now
        // in the future, new columns may appear
        // like labels, bookmarks, etc...
        // tocTV->header()->hide();
        tocTV->header()->setVisible(false);
-       enableButtons();
+       enableButtons(buttonsEnabled);
 
        reconnectSelectionModel();
        depthSL->setEnabled(true);

Reply via email to