Results 1 to 3 of 3

Thread: QListWidget back to previous item in currentItemChanged

  1. #1
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget back to previous item in currentItemChanged

    Hi all,

    I found an interesting problem. I use a QListWidget for selection between items. I would like to ask the user if he is sure to change the current item. My plan is to use the currentItemChanged signal of the QListWidget, because it has the previous item pointer.

    Sample code:
    Qt Code:
    1. #include "mainwidget.h"
    2.  
    3. #include <QListWidget>
    4. #include <QGridLayout>
    5.  
    6. #include <QMessageBox>
    7. #include <QTimer>
    8.  
    9. MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
    10. {
    11. list = new QListWidget(this);
    12.  
    13. QGridLayout* mainLayout = new QGridLayout();
    14. mainLayout->addWidget(list);
    15. setLayout(mainLayout);
    16.  
    17. list->addItem("Item1");
    18. list->addItem("Item2");
    19. list->addItem("Item3");
    20. list->addItem("Item4");
    21.  
    22. connect(list, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(currentItemChanged(QListWidgetItem*,QListWidgetItem*)));
    23. }
    24.  
    25. MainWidget::~MainWidget()
    26. {
    27. delete list;
    28. }
    29.  
    30. void MainWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
    31. {
    32. qDebug("CurrentItemChanged - Index: %d", list->row(current));
    33. if ( list->row(current) == currentRow )
    34. {
    35. return;
    36. }
    37. if ( QMessageBox::No == QMessageBox::question(this, "Question", "Really want to change?", QMessageBox::Yes | QMessageBox::No) )
    38. {
    39. //TODO: change back to the previous item
    40. list->setCurrentItem(previous);
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    That was my first idea, but of course it asks again the question, so I tried to temporally disable signals

    Qt Code:
    1. if ( QMessageBox::No == QMessageBox::question(this, "Question", "Really want to change?", QMessageBox::Yes | QMessageBox::No) )
    2. {
    3. //TODO: change back to the previous item
    4. list->blockSignals(true);
    5. list->setCurrentItem(previous);
    6. list->blockSignals(false);
    7. }
    To copy to clipboard, switch view to plain text mode 

    The problem with this is that the current item changes back to the previous item, but the selection not and I don't really understand this behaviour.
    For a workaround, I have this solution, but I think it's not very elegant.
    Qt Code:
    1. void MainWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
    2. {
    3. qDebug("CurrentItemChanged - Index: %d", list->row(current));
    4. if ( list->row(current) == currentRow )
    5. {
    6. return;
    7. }
    8. if ( QMessageBox::No == QMessageBox::question(this, "Question", "Really want to change?", QMessageBox::Yes | QMessageBox::No) )
    9. {
    10. //TODO: change back to the previous item
    11. QTimer::singleShot(0, this, SLOT(goBack()));
    12. }else
    13. {
    14. currentRow = list->row(current);
    15. }
    16. }
    17.  
    18. void MainWidget::goBack()
    19. {
    20. qDebug("GoBack: %d", currentRow);
    21. list->setCurrentRow(currentRow);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Do You have any explanation why this workaround works, and why the selection doesn't go back if I call it from the slot directly? Is it normal? Do You have any idea for a more elegant solution?

    Thanks!

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListWidget back to previous item in currentItemChanged

    You need to update selection model as well and because you block the signals model doesn't get updated.

    Anyway, another solution could be to install event filter on the List widget, and capture mouse release or key up/down (whatever lets you change current item).
    There you can show your dialog and if the user cancels it, you can return true to filter out the event and prevent anything from happening.

  3. #3
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget back to previous item in currentItemChanged

    Thanks, I will try to update the selection model also.

Similar Threads

  1. Replies: 3
    Last Post: 17th May 2012, 02:27
  2. Replies: 0
    Last Post: 8th May 2011, 14:36
  3. can not go back to the previous window
    By xhsoldier in forum Qt Programming
    Replies: 1
    Last Post: 20th October 2010, 05:45
  4. Replies: 1
    Last Post: 25th February 2009, 00:34
  5. Replies: 12
    Last Post: 14th June 2006, 09:24

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.