Results 1 to 6 of 6

Thread: Have a single selection of item between 2 QListWidgets

  1. #1
    Join Date
    Jan 2014
    Posts
    9
    Qt products
    Platforms
    MacOS X Unix/X11

    Default Have a single selection of item between 2 QListWidgets

    Hi all, I am pretty new to PyQt but nevertheless currently I am encountering this problem, and I hope someone can help me.

    I am trying to create a situation where there are 2 QListWidgets, List01 and List02 for example, and they contains the following.
    List01 = [T01, T02, T03]
    List02 = [P01, P02, P03]
    I wanted the user to select an item (T01) in List01, and hence in List02, no selection (highlighting) of any items will be conducted, meaning to say if the user hovers over to List02 and selects an item (P02), the selection in List01 will be gone and it will be the item (P02) selected in List02.

    Currently, I am getting the problem, where my program is able to select an item in the 2 lists.

    Could someone kindly guide me?
    Many thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Have a single selection of item between 2 QListWidgets

    You react to the selection change signals of both lists and clear the selection of the other.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    9
    Qt products
    Platforms
    MacOS X Unix/X11

    Default Re: Have a single selection of item between 2 QListWidgets

    Quote Originally Posted by anda_skoa View Post
    You react to the selection change signals of both lists and clear the selection of the other.

    Cheers,
    _
    Hi anda, could you kindly further elaborate on that? I can somewhat understand the latter part but the first part of the reacting to change signals, could you kindly state a simple example for me?
    Sorry about that...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Have a single selection of item between 2 QListWidgets

    QListWidget has a signal called itemSelectionChanged().

    Create two slots, one that is connected to the first list's signal and one that is connected to the second list's signal.
    When the slots are invoked, they clear the other list's selection.
    Since clearing the selection might also result in the signal being emitted, you need to take care of that, e.g. by setting a flag and ignoring the slot invokation while it is set. In both slots obviously.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    9
    Qt products
    Platforms
    MacOS X Unix/X11

    Default Re: Have a single selection of item between 2 QListWidgets

    Quote Originally Posted by anda_skoa View Post
    QListWidget has a signal called itemSelectionChanged().

    Create two slots, one that is connected to the first list's signal and one that is connected to the second list's signal.
    When the slots are invoked, they clear the other list's selection.
    Since clearing the selection might also result in the signal being emitted, you need to take care of that, e.g. by setting a flag and ignoring the slot invokation while it is set. In both slots obviously.

    Cheers,
    _
    Okay I will try out your method and see how it goes...
    Nonetheless, I currently have a code below, it seems to be working for me, but could I get any advices if it is good enough?

    Qt Code:
    1. from PyQt4.QtGui import *
    2. app = QApplication([])
    3. widget = QWidget()
    4. mainLayout = QHBoxLayout(widget)
    5. widget.setLayout(l)
    6.  
    7. list01 = QListWidget()
    8. list02 = QListWidget()
    9.  
    10. list01.addItems(["1","2","3"])
    11. list02.addItems(["4","5","6"])
    12.  
    13. def test01():
    14. list02.itemSelectionChanged.disconnect(test02)
    15. for item in list02.selectedItems():
    16. list02.setItemSelected(item,False)
    17. list02.itemSelectionChanged.connect(test02)
    18.  
    19.  
    20. def test02():
    21. list01.itemSelectionChanged.disconnect(test01)
    22. for item in list01.selectedItems():
    23. list01.setItemSelected(item,False)
    24. list01.itemSelectionChanged.connect(test01)
    25.  
    26. print dir(list01.itemSelectionChanged)
    27.  
    28. list01.itemSelectionChanged.connect(test01)
    29. list02.itemSelectionChanged.connect(test02)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Have a single selection of item between 2 QListWidgets

    Yes, that looks ok.

    Alternatively to the loop you could also try

    Qt Code:
    1. list01.selectionModel().clear()
    To copy to clipboard, switch view to plain text mode 
    respectively for list02

    Cheers,
    _

Similar Threads

  1. Isolating update of a single item
    By sp in forum Qt Programming
    Replies: 6
    Last Post: 8th August 2013, 07:09
  2. Replies: 0
    Last Post: 11th June 2013, 10:50
  3. Styling a Single Item on a QMenuBar
    By Goug in forum Qt Programming
    Replies: 3
    Last Post: 10th November 2011, 07:04
  4. Single Item update in QGraphicsView
    By oberlus in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2010, 09:17
  5. Replies: 14
    Last Post: 9th November 2006, 08:35

Tags for this Thread

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.