Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-6237

Two connected QSortFilterProxyModel(s) can crash an application

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P1: Critical
    • 4.6.1
    • 4.5.3, 4.6.0
    • Widgets: Itemviews
    • None
    • Windows XP - MSVC 2005, MSVC 2008. Linux as well.
    • e8d7d2172627dd3cac8b0cc3165ed371b524feb4

    Description

      The following code reproduces the crash.
      When no items are selected or the root item is selected the button will swap the source models.
      If some of the child items are selected the application will crash.


      #include <QtGui>
      
      class Widget : public QWidget {
          Q_OBJECT
      
      public slots:
          void swap();
      
      public:
          Widget(QWidget *parent = 0);
      
      private:
          QTreeView *treeView;
          QStandardItemModel *model1;
          QStandardItemModel *model2;
          QSortFilterProxyModel *proxyModel;
          QSortFilterProxyModel *toggleProxy;
      };
      
      Widget::Widget(QWidget *parent) : QWidget(parent)
      {
          model1 = new QStandardItemModel;
          QStandardItem *parentItem = model1->invisibleRootItem();
          for (int i = 0; i < 4; ++i) {
              QStandardItem *item = new QStandardItem(QString("model1 item %0").arg(i));
              parentItem->appendRow(item);
              parentItem = item;
          }
      
          model2 = new QStandardItemModel;
          QStandardItem *parentItem2 = model2->invisibleRootItem();
          for (int i = 0; i < 4; ++i) {
              QStandardItem *item = new QStandardItem(QString("model2 item %0").arg(i));
              parentItem2->appendRow(item);
              parentItem2 = item;
          }
      
          toggleProxy = new QSortFilterProxyModel;
          toggleProxy->setSourceModel(model1);
      
          proxyModel = new QSortFilterProxyModel;
          proxyModel->setSourceModel(toggleProxy);
      
          QHBoxLayout *layout = new QHBoxLayout;
          treeView = new QTreeView;
      //    treeView->setModel(toggleProxy);
          treeView->setModel(proxyModel);
          layout->addWidget(treeView);
          QPushButton *swapButton = new QPushButton("Swap");
          layout->addWidget(swapButton);
          setLayout(layout);
      
          connect(swapButton, SIGNAL(clicked()), this, SLOT(swap()));
      }
      
      void Widget::swap()
      {
          if (toggleProxy->sourceModel() == model1)
              toggleProxy->setSourceModel(model2);
          else
              toggleProxy->setSourceModel(model1);
      }
      
      #include "main.moc"
      
      int main(int argc, char **argv)
      {
          QApplication a(argc, argv);
          Widget w;
          w.show();
          return a.exec();
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            dedietri Gabriel de Dietrich (drgvond)
            mpejcoch Martin Pejcoch (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes