Results 1 to 7 of 7

Thread: QListWidget read only

  1. #1
    Join Date
    Mar 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget read only

    Hello,

    I want a QListWidget that the user can't change the selected index but he can scroll into the list.
    An other thing, i should be able to change the selected index in code behind.
    I think about a setEnabled(false) on my QListWidget, but with this the user can't scroll.
    Any ideas?

    Thanks.

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

    Default Re: QListWidget read only

    You can catch mouse events and suppress them when needed. This way the user can't select items but still will be able to scroll.

  3. #3
    Join Date
    Mar 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget read only

    Mouse events on QListWidget or on QListWidgetItem?

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

    Default Re: QListWidget read only

    Whatever works.

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

    Default Re: QListWidget read only

    There's many ways you can get what you want.

    If you need only mouse events to be ignored then you can use this:
    Qt Code:
    1. this->list->viewport()->setAttribute( Qt::WA_TransparentForMouseEvents );
    To copy to clipboard, switch view to plain text mode 
    This will not stop user from changing the selection using keyboard though.

    To fully suppress user interaction with the widget install an event filter which will stop all mouse events on the viewport and keyboard events going to the widget:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. list = new QListWidget( this );
    5.  
    6. [...]// setup your lsit
    7.  
    8. this->list->installEventFilter( this );
    9. this->list->viewport()->installEventFilter( this );
    10. }
    11.  
    12. bool MainWindow::eventFilter( QObject* o, QEvent* e )
    13. {
    14. bool ret = false;
    15.  
    16. if( o == this->list &&
    17. ( e->type() == QEvent::KeyPress
    18. || e->type() == QEvent::KeyRelease ) )
    19. {
    20. qDebug() << "key";
    21. ret = true;
    22. }
    23. else if( o == this->list->viewport() &&
    24. ( e->type() == QEvent::MouseButtonPress
    25. || e->type() == QEvent::MouseButtonPress
    26. || e->type() == QEvent::MouseButtonPress ) )
    27. {
    28. qDebug() << "mouse";
    29. ret = true;
    30. }
    31.  
    32. return ret;
    33. }
    To copy to clipboard, switch view to plain text mode 
    Another way could be disabling the list widget and sticking it in separate scroll area.
    Yet another would be setting NoSelection on the widget and providing custom item delegate which would draw item as 'selected' based on a property set on the item.

  6. #6
    Join Date
    Mar 2012
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget read only

    Thank you Spitfire. it works with this modifications:

    Qt Code:
    1. bool WorkListViewUI::eventFilter(QObject* o, QEvent* e )
    2. {
    3. bool ret = false;
    4.  
    5. if( o == pListView &&
    6. ( e->type() == QEvent::KeyPress
    7. || e->type() == QEvent::KeyRelease ) )
    8. {
    9. qDebug() << "key";
    10. ret = true;
    11. }
    12. else if( o == pListView->viewport() &&
    13. ( e->type() == QEvent::MouseButtonPress
    14. || e->type() == QEvent::MouseButtonRelease
    15. || e->type() == QEvent::MouseMove
    16. || e->type() == QEvent::MouseButtonDblClick ) )
    17. {
    18. qDebug() << "mouse";
    19. ret = true;
    20. }
    21.  
    22. return ret;
    23. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    May 2013
    Posts
    6
    Thanks
    2

    Default Re: QListWidget read only

    or easy QAbstractItemView::setEventTrigger with QAbstractItemView::NoEditTriggers

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. Read SMS in-box content & show it in QListWidget?
    By damodharan in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 05:51
  4. Another QListWidget bug?
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2006, 13:40
  5. Possible bug of Qt on QListWidget?
    By SkripT in forum Qt Programming
    Replies: 9
    Last Post: 28th March 2006, 13:05

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
  •  
Qt is a trademark of The Qt Company.