Results 1 to 6 of 6

Thread: QItemSelectionModel::currentChanged and revert change

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default QItemSelectionModel::currentChanged and revert change

    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.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QItemSelectionModel::currentChanged and revert change

    looks and works fine. maybe I don't understand something.
    when I selected an item a message box appeared. I pressed "cancel" and cursor has been set on previous item.
    edited: ah, I see. when mouse is used then reverting is wrong. works fine for keyboard.
    Last edited by spirit; 24th March 2009 at 16:17. Reason: updated contents
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QItemSelectionModel::currentChanged and revert change

    looks like this works
    h-file
    Qt Code:
    1. ...
    2. public slots:
    3. void changed( const QModelIndex & current, const QModelIndex & previous );
    4. void restoreSlot(const QModelIndex &index);
    5.  
    6. signals:
    7. void restore(const QModelIndex &index);
    8. ...
    To copy to clipboard, switch view to plain text mode 
    cpp-file
    Qt Code:
    1. ...
    2. connect(this, SIGNAL(restore(const QModelIndex &)), this, SLOT(restoreSlot(const QModelIndex &)), Qt::QueuedConnection);
    3. ...
    4. void Test::changed( const QModelIndex & current, const QModelIndex & previous )
    5. {
    6. qWarning() << current << previous;
    7. if (canceled)
    8. {
    9. canceled = false;
    10. return;
    11. }
    12.  
    13. QMessageBox msgBox;
    14. msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
    15. int ret = msgBox.exec();
    16. switch (ret)
    17. {
    18. case QMessageBox::Discard:
    19. break;
    20. case QMessageBox::Cancel:
    21. canceled = true;
    22. //v->selectionModel()->setCurrentIndex(previous, QItemSelectionModel::ClearAndSelect); // no "back change"
    23. //v->setCurrentIndex(previous); // all lines between current and previous are selected
    24. emit restore(previous);
    25. return;
    26. break;
    27. }
    28. }
    29.  
    30. void Test::restoreSlot(const QModelIndex &index)
    31. {
    32. v->setCurrentIndex(index);
    33. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. The following user says thank you to spirit for this useful post:

    Lykurg (24th March 2009)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QItemSelectionModel::currentChanged and revert change

    It's amazing how fast this board is! I was just out for spot, came back, got an answer, tried it, works fine, thanks!

    The problem is also that qt needs time to revers the selection because without Qt::QueuedConnection it will fail. And it's curios that it doesn't respect the QAbstractItemView::SingleSelection. Is this a thing one could not change or maybe the trolls could? Should we inform them? I don't know, I don't understand all of the issue exact.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QItemSelectionModel::currentChanged and revert change

    Ok, the "real" problem was the signal-slot binding. If i directly use
    Qt Code:
    1. connect(v->selectionModel(), SIGNAL(currentChanged(const QModelIndex& , const QModelIndex&)), this, SLOT(changed(const QModelIndex& , const QModelIndex&)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    all works as expected without the additional signal.

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QItemSelectionModel::currentChanged and revert change

    nice, I've just posted a solution without investigation.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Replies: 1
    Last Post: 6th January 2009, 14:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.