Hi,

I have a QListView and connect the signal from its selection model ItemSelectionModel::currentChanged(const QModelIndex &current, const QModelIndex &previous) to my slot. In that slot I want to revert the change. But I fail. How to revert an selection?

Example:
Qt Code:
  1. #include <QtGui>
  2. #include "test.h"
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. QApplication app(argc, argv);
  7.  
  8. Test w;
  9. w.show();
  10.  
  11. return app.exec();
  12. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #ifndef TEST_H
  2. #define TEST_H
  3.  
  4. #include <QtGui>
  5.  
  6. class Test : public QWidget
  7. {
  8. Q_OBJECT
  9. public:
  10. Test(QWidget *parent = 0);
  11. bool canceled;
  12.  
  13. public slots:
  14. void changed( const QModelIndex & current, const QModelIndex & previous );
  15. };
  16. #endif
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "test.h"
  2. #include <QtGui>
  3.  
  4. Test::Test(QWidget *parent)
  5. : QWidget(parent)
  6. {
  7. QLayout *l = new QHBoxLayout(this);
  8. v = new QListView(this);
  9. v->setSelectionMode(QAbstractItemView::SingleSelection);
  10. list << "a" << "b" << "c" << "d" << "e" << "f";
  11. model->setStringList(list);
  12. v->setModel(model);
  13. l->addWidget(v);
  14. setLayout(l);
  15. v->setCurrentIndex(model->index(0,0));
  16. connect(v->selectionModel(), SIGNAL(currentChanged(const QModelIndex& , const QModelIndex&)), this, SLOT(changed(const QModelIndex& , const QModelIndex&)));
  17. canceled = false;
  18. }
  19.  
  20. void Test::changed( const QModelIndex & current, const QModelIndex & previous )
  21. {
  22. qWarning() << current << previous;
  23. if (canceled)
  24. {
  25. canceled = false;
  26. return;
  27. }
  28.  
  29. QMessageBox msgBox;
  30. msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
  31. int ret = msgBox.exec();
  32. switch (ret)
  33. {
  34. case QMessageBox::Discard:
  35. break;
  36. case QMessageBox::Cancel:
  37. canceled = true;
  38. v->selectionModel()->setCurrentIndex(previous, QItemSelectionModel::ClearAndSelect); // no "back change"
  39. //v->setCurrentIndex(previous); // all lines between current and previous are selected
  40. return;
  41. break;
  42. }
  43. }
To copy to clipboard, switch view to plain text mode 

Thanks for any help.