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 and I am not sure how to perform the above.
Could someone kindly guide me?
Many thanks in advance
OK here is an example code of how could you do what you want, it's very basic but you can get the idea within the functions f and g, hope it works:
import PyQt4.QtGui as gui
app = gui.QApplication([])
w = gui.QWidget()
l = gui.QHBoxLayout(w)
w.setLayout(l)
lis1 = gui.QListWidget()
lis2 = gui.QListWidget()
lis1.addItems(["1","2","3"])
lis2.addItems(["4","5","6"])
def f():
lis2.itemSelectionChanged.disconnect(g)
for item in lis2.selectedItems():
lis2.setItemSelected(item,False)
lis2.itemSelectionChanged.connect(g)
def g():
lis1.itemSelectionChanged.disconnect(f)
for item in lis1.selectedItems():
lis1.setItemSelected(item,False)
lis1.itemSelectionChanged.connect(f)
print dir(lis1.itemSelectionChanged)
lis1.itemSelectionChanged.connect(f)
lis2.itemSelectionChanged.connect(g)
l.addWidget(lis1)
l.addWidget(lis2)
w.show()
app.exec_()
Connect the itemSelectionChanged() signal from one QListWidget to the clearSelection slot of the other.
Example generated with QtDesigner:
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(214, 158)
self.gridLayout = QtGui.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.listWidget = QtGui.QListWidget(Form)
self.listWidget.setObjectName("listWidget")
item = QtGui.QListWidgetItem()
item.setText("T01")
self.listWidget.addItem(item)
item = QtGui.QListWidgetItem()
item.setText("T02")
self.listWidget.addItem(item)
item = QtGui.QListWidgetItem()
item.setText("T03")
self.listWidget.addItem(item)
self.gridLayout.addWidget(self.listWidget, 0, 0, 1, 1)
self.listWidget_2 = QtGui.QListWidget(Form)
self.listWidget_2.setObjectName("listWidget_2")
item = QtGui.QListWidgetItem()
item.setText("P01")
self.listWidget_2.addItem(item)
item = QtGui.QListWidgetItem()
item.setText("P02")
self.listWidget_2.addItem(item)
item = QtGui.QListWidgetItem()
item.setText("P03")
self.listWidget_2.addItem(item)
self.gridLayout.addWidget(self.listWidget_2, 0, 1, 1, 1)
# This are the important lines.
QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL("itemSelectionChanged()"), self.listWidget_2.clearSelection)
QtCore.QObject.connect(self.listWidget_2, QtCore.SIGNAL("itemSelectionChanged()"), self.listWidget.clearSelection)
QtCore.QMetaObject.connectSlotsByName(Form)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
You can take example on this code:
from PyQt4 import QtCore, QtGui
import sys
app = QtGui.QApplication(sys.argv)
class MyApp(QtGui.QDialog):
def __init__(self):
super(MyApp, self).__init__()
layout = QtGui.QHBoxLayout()
qlist1 = QtGui.QListWidget()
qlist1.addItems(["elem1","elem2","elem3"])
layout.addWidget(qlist1)
qlist2 = QtGui.QListWidget()
qlist2.addItems(["elem4","elem5","elem6"])
layout.addWidget(qlist2)
# This dict will be used when a list is clicked
# to clear the selection of the other list
self.list_dict = {}
self.list_dict[qlist1] = qlist2
self.list_dict[qlist2] = qlist1
qlist1.clicked.connect(self.list_clicked)
qlist2.clicked.connect(self.list_clicked)
self.setLayout(layout)
self.show()
def list_clicked(self):
self.list_dict[self.sender()].clearSelection()
myApp = MyApp()
sys.exit(app.exec_())
Related
I'm stuck using myItem.hide() method every time I need to remove Item from QListWidget list. Hiding an item instead of deleting/removing makes things unnecessary complex. I would appreciate if you show me how to delete Item from ListWidget permanently.
from PyQt4 import QtGui, QtCore
class MyApp(object):
def __init__(self):
super(MyApp, self).__init__()
self.mainWidget = QtGui.QWidget()
self.mainLayout = QtGui.QVBoxLayout()
self.mainWidget.setLayout(self.mainLayout)
self.hLayout = QtGui.QHBoxLayout()
self.mainLayout.insertLayout(0, self.hLayout)
self.listA=QtGui.QListWidget()
for i in range(3):
self.listA.addItem('Item '+str(i))
self.hLayout.addWidget(self.listA)
self.buttonGroupbox = QtGui.QGroupBox()
self.buttonlayout = QtGui.QVBoxLayout()
self.buttonGroupbox.setLayout(self.buttonlayout)
okButton = QtGui.QPushButton('Remove Selected')
okButton.clicked.connect(self.removeSel)
self.buttonlayout.addWidget(okButton)
self.mainLayout.addWidget(self.buttonGroupbox)
self.mainWidget.show()
sys.exit(app.exec_())
def removeSel(self):
listItems=self.listA.selectedItems()
if not listItems: return
for item in listItems:
print type(item), dir(item)
I don't know why but removeItemWidget don't work as expected. You have to use take item instead:
def removeSel(self):
listItems=self.listA.selectedItems()
if not listItems: return
for item in listItems:
self.listA.takeItem(self.listA.row(item))
Posting here an example showing how to implement same approach but now applied to QTreeWidget which a bit more involved than QListWidget.
from PyQt4 import QtGui, QtCore
class MyApp(object):
def __init__(self):
super(MyApp, self).__init__()
self.mainWidget = QtGui.QWidget()
self.mainLayout = QtGui.QVBoxLayout()
self.mainWidget.setLayout(self.mainLayout)
self.hLayout = QtGui.QHBoxLayout()
self.mainLayout.insertLayout(0, self.hLayout)
self.listA=QtGui.QTreeWidget()
self.listA.setColumnCount(3)
self.listA.setHeaderLabels(['Checkbox','Name','Data'])
for i in range(3):
item=QtGui.QTreeWidgetItem()
item.setCheckState(0,QtCore.Qt.Checked)
item.setText(1, 'Item '+str(i))
item.setData(2, QtCore.Qt.UserRole, id(item) )
item.setText(2, str(id(item) ) )
self.listA.addTopLevelItem(item)
self.hLayout.addWidget(self.listA)
self.buttonGroupbox = QtGui.QGroupBox()
self.buttonlayout = QtGui.QVBoxLayout()
self.buttonGroupbox.setLayout(self.buttonlayout)
okButton = QtGui.QPushButton('Remove Selected')
okButton.clicked.connect(self.removeSel)
self.buttonlayout.addWidget(okButton)
getDataButton = QtGui.QPushButton('Get Items Data')
getDataButton.clicked.connect(self.getItemsData)
self.buttonlayout.addWidget(getDataButton)
self.mainLayout.addWidget(self.buttonGroupbox)
self.mainWidget.show()
sys.exit(app.exec_())
def removeSel(self):
listItems=self.listA.selectedItems()
if not listItems: return
for item in listItems:
itemIndex=self.listA.indexOfTopLevelItem(item)
self.listA.takeTopLevelItem(itemIndex)
print '\n\t Number of items remaining', self.listA.topLevelItemCount()
def getItemsData(self):
for i in range(self.listA.topLevelItemCount()):
item=self.listA.topLevelItem(i)
itmData=item.data(2, QtCore.Qt.UserRole)
itemId=itmData.toPyObject()
print '\n\t Item Id Stored as Item Data:', itemId, 'Item Checkbox State:', item.checkState(0)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
MyApp()
A ListWidget is a list of ListWidgetItems. A ListWidgetItems can be assigned a custom widget to override the default, so removeItemWidget() only removes the custom widget. Hence the need for takeItem, which pops the item from the list and returns it (similar to how a python list works)
I've spent a lot of time making GUIs using tkinter, but never used the OOP style so I wanted to start learning PyQt5 and in turn start learning OOP (definitely struggling through it).
I currently have three files:
app.py - "main entry point" for the application
main_menu.py - Python file converted from the PyQt designer .ui file
add_functions.py - file that will have functions to update GUI based on user actions and run functions from button clicks
My question is, how do I update the initial values of the combo boxes, outside of the "main_menu.py" file where the widgets are created? I'm assuming I do it somewhere in the "app.py" file, but I cannot figure it out.
The code below doesn't throw any errors, but the combo boxes are blank where one should be a list of strings and the other a single string (will later be updated based on first combo box selection)
I was able to get it to work with everything basically in the "main_menu.py" file, but then I realized every time I want to change the UI, it would create a problem.
app.py
from PyQt5.QtWidgets import QMainWindow
from PyQt5 import QtWidgets
# import GUI from designer file
from main_menu import Ui_main_menu
# import other functions
from add_functions import ChangeLists
class Main(QMainWindow, Ui_main_menu):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
# Team List
team_list_file = open(r'C:\NHLdb_pyqt\files\NHLteams.txt', 'r')
team_list = team_list_file.read().splitlines()
team_list_file.close()
print("team list: ", team_list)
# Initial Player List
player_list_init = "Please Select a Team"
# update combo box lists
print("team list: ", team_list)
self.team_select_combobox.addItems(team_list)
self.player_select_combobox.addItem(player_list_init)
# connect combo box to function that will change player list based on team list selection
# self.team_select_combobox.currentTextChanged.connect(ChangeLists.team_changed)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_menu = QtWidgets.QDialog()
ui = Ui_main_menu()
ui.setupUi(main_menu)
main_menu.show()
sys.exit(app.exec_())
main_menu.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main_menu(object):
def setupUi(self, main_menu):
main_menu.setObjectName("main_menu")
main_menu.resize(463, 408)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(main_menu)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.tabWidget = QtWidgets.QTabWidget(main_menu)
self.tabWidget.setObjectName("tabWidget")
self.get_data_tab = QtWidgets.QWidget()
self.get_data_tab.setObjectName("get_data_tab")
self.verticalLayout = QtWidgets.QVBoxLayout(self.get_data_tab)
self.verticalLayout.setObjectName("verticalLayout")
self.team_player_layout = QtWidgets.QGridLayout()
self.team_player_layout.setObjectName("team_player_layout")
self.player_select_label = QtWidgets.QLabel(self.get_data_tab)
self.player_select_label.setAlignment(QtCore.Qt.AlignCenter)
self.player_select_label.setObjectName("player_select_label")
self.team_player_layout.addWidget(self.player_select_label, 2, 0, 1, 1)
self.team_select_combobox = QtWidgets.QComboBox(self.get_data_tab)
self.team_select_combobox.setCurrentText("")
self.team_select_combobox.setPlaceholderText("")
self.team_select_combobox.setObjectName("team_select_combobox")
self.team_player_layout.addWidget(self.team_select_combobox, 0, 1, 1, 1)
self.player_select_combobox = QtWidgets.QComboBox(self.get_data_tab)
self.player_select_combobox.setObjectName("player_select_combobox")
self.team_player_layout.addWidget(self.player_select_combobox, 2, 1, 1, 1)
self.team_select_label = QtWidgets.QLabel(self.get_data_tab)
self.team_select_label.setAlignment(QtCore.Qt.AlignCenter)
self.team_select_label.setObjectName("team_select_label")
self.team_player_layout.addWidget(self.team_select_label, 0, 0, 1, 1)
self.get_player_data_button = QtWidgets.QPushButton(self.get_data_tab)
self.get_player_data_button.setObjectName("get_player_data_button")
self.team_player_layout.addWidget(self.get_player_data_button, 3, 0, 1, 2)
self.verticalLayout.addLayout(self.team_player_layout)
self.tableWidget = QtWidgets.QTableWidget(self.get_data_tab)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.verticalLayout.addWidget(self.tableWidget)
self.tabWidget.addTab(self.get_data_tab, "")
self.view_db_tab = QtWidgets.QWidget()
self.view_db_tab.setObjectName("view_db_tab")
self.tabWidget.addTab(self.view_db_tab, "")
self.horizontalLayout_2.addWidget(self.tabWidget)
self.retranslateUi(main_menu)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(main_menu)
def retranslateUi(self, main_menu):
_translate = QtCore.QCoreApplication.translate
main_menu.setWindowTitle(_translate("main_menu", "NHL Database"))
self.player_select_label.setText(_translate("main_menu", "Select A Player:"))
self.team_select_label.setText(_translate("main_menu", "Select A Team:"))
self.get_player_data_button.setText(_translate("main_menu", "Get Player Data"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("main_menu", "Year"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("main_menu", "Team"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("main_menu", "League"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("main_menu", "GP"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.get_data_tab), _translate("main_menu", "Get Player Data"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.view_db_tab), _translate("main_menu", "View Database"))
add_functions
from os import path
from PyQt5 import QtCore, QtGui, QtWidgets
class ChangeLists:
def team_changed(self):
index = self.team_select_combobox.currentText()
if index != 0 and path.exists(r'C:\NHLdb\files\_' + str(index) + '.txt'):
player_list_file = open(r'C:\NHLdb\files\_' + str(index) + '.txt', 'r')
player_list_update = player_list_file.read().splitlines()
player_list_file.close()
self.player_select_combobox.clear()
self.player_select_combobox.addItems(player_list_update)
if index != 0 and not path.exists(r'C:\NHLdb\files\_' + str(index) + '.txt'):
self.player_select_combobox.clear()
self.player_select_combobox.addItem("Prospects Not Compiled")
Your calling order in the app.py is a bit off. Class Main is never called and hence your intialization routine defined there never gets executed. Additionally, class Main inherits from the wrong Window type. In your case, you designed a QDialog, so it should inherit from that (instead of QMainWindow).
Upon fixing these issues, everything you defined in the Main class should work as expected:
modified app.py
from PyQt5 import QtWidgets
from main_menu import Ui_main_menu
class Main(QtWidgets.QDialog, Ui_main_menu):
def __init__(self):
super().__init__()
# setup ui
self.setupUi(self)
# Team List
team_list_file = open(r'D:\temp.txt', 'r')
team_list = team_list_file.read().splitlines()
team_list_file.close()
# Initial Player List
player_list_init = "Please Select a Team"
# update combo box lists
self.team_select_combobox.addItems(team_list)
self.player_select_combobox.addItem(player_list_init)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
ui = Main()
ui.show()
app.exec_()
I am trying to dynamically update a menu with new items when I add new items via the form into Qsettings. For example, if you open my code and click the button and then click "new" it will open a QLineEdit with a button. When the button is clicked the list data gets stored via Qsettings.
I'd like to be able to update the menu somehow to show the items without restarting. I tried some things like calling repaint and update in a few areas with no luck.
Here is my code, I slimmed it down the best that I could for the example.
import functools
import sys
from PyQt5 import QtCore
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout, \
QVBoxLayout, QLineEdit,QApplication, QWidgetAction, QTextBrowser, QAction, QMenu
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.layout = QHBoxLayout()
self.menu_action = QAction(QIcon("icon.png"),"New", self)
self.menu_btn = QPushButton()
self.menu = MyMenu("Menu", self.menu_btn)
self.add_menu = self.menu.addMenu(QIcon("icon.png"), "Menu")
self.add_menu.addAction(self.menu_action)
self.menu_btn.setMenu(self.menu)
self.textBox = QTextBrowser(self)
action = QWidgetAction(self.menu_btn)
action.setDefaultWidget(self.textBox)
self.menu_btn.menu().addAction(action)
settings = QtCore.QSettings('test_org', 'my_app')
self.new_items = settings.value('new_item', [])
print('%s' % self.new_items)
for item in self.new_items:
self.create_action = QAction(QIcon("icon.png"), item[0], self)
self.create_action.setData(item)
self.add_menu.addAction(self.create_action)
self.create_action.triggered.connect(functools.partial(self.menu_clicked, self.create_action))
self.layout.addWidget(self.menu_btn)
self.setLayout(self.layout)
self.menu_action.triggered.connect(self.open_window)
def open_window(self):
self.create_menu_item = Create_Menu_Item()
self.create_menu_item.show()
def menu_clicked(self, item):
itmData = item.data()
print(itmData)
class Create_Menu_Item(QWidget):
def __init__(self, parent=None):
super(Create_Menu_Item, self).__init__(parent)
self.resize(200, 195)
self.layout = QVBoxLayout()
self.form = QLineEdit()
self.btn = QPushButton()
self.layout.addWidget(self.form)
self.layout.addWidget(self.btn)
self.btn.clicked.connect(self.save_new_item)
self.setLayout(self.layout)
def save_new_item(self):
settings = QtCore.QSettings('test_org', 'my_app')
self.new_item = settings.value('new_item', [])
self.new_item.append([self.form.text()])
settings.setValue('new_item', self.new_item)
print(self.new_item)
print('saved!')
class MyMenu(QMenu):
def event(self,event):
if event.type() == QtCore.QEvent.Show:
self.move(self.parent().mapToGlobal(QtCore.QPoint(0,0))-QtCore.QPoint(0,self.height()))
return super(MyMenu,self).event(event)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
Anyone have any ideas? Thanks.
You can create a for loop to access the list.
Bare in mind it is a list of lists so a nested for loop is neccessary
def save_new_item(self):
settings = QtCore.QSettings('test_org', 'my_app')
self.new_item = settings.value('new_item', [])
self.new_item.append([self.form.text()])
settings.setValue('new_item', self.new_item)
print(self.new_item)
print('saved!')
# Add new menu items..
for x in self.new_item:
for y in x:
print(y)
The above will keep adding the WHOLE list of items every time you add a new item..
To just add the newest item, this is all you need (the last item added to the list)
w.add_menu.addAction(self.new_item[0][-1])
so
def save_new_item(self):
settings = QtCore.QSettings('test_org', 'my_app')
self.new_item = settings.value('new_item', [])
self.new_item.append([self.form.text()])
settings.setValue('new_item', self.new_item)
print(self.new_item)
print('saved!')
#ADD last item in the list
w.add_menu.addAction(self.new_item[0][-1])
How can I add customized items to a QListWidget with a background color that I choose, and add a bottom border to each item, like this draft example in the picture below.
This is the code that I wrote:
from PyQt5 import QtWidgets, QtGui
import sys
class CustomListHead(QtWidgets.QWidget):
def __init__(self):
super(CustomListHead, self).__init__()
self.project_title = QtWidgets.QLabel("Today")
self.set_ui()
def set_ui(self):
grid_box = QtWidgets.QGridLayout()
grid_box.addWidget(self.project_title, 0, 0)
self.setLayout(grid_box)
self.show()
class CustomListItem(QtWidgets.QWidget):
def __init__(self):
super(CustomListItem, self).__init__()
self.project_title = QtWidgets.QLabel("Learn Python")
self.task_title = QtWidgets.QLabel("Learn more about forms, models and include")
self.set_ui()
def set_ui(self):
grid_box = QtWidgets.QGridLayout()
grid_box.addWidget(self.project_title, 0, 0)
grid_box.addWidget(self.task_title, 1, 0)
self.setLayout(grid_box)
self.show()
class MainWindowUI(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindowUI, self).__init__()
self.list_widget = QtWidgets.QListWidget()
self.set_ui()
def set_ui(self):
custom_head_item = CustomListHead()
item = QtWidgets.QListWidgetItem(self.list_widget)
item.setSizeHint(custom_head_item.sizeHint())
self.list_widget.setItemWidget(item, custom_head_item)
self.list_widget.addItem(item)
custom_item = CustomListItem()
item = QtWidgets.QListWidgetItem(self.list_widget)
item.setSizeHint(custom_item.sizeHint())
self.list_widget.addItem(item)
self.list_widget.setItemWidget(item, custom_item)
vertical_layout = QtWidgets.QVBoxLayout()
vertical_layout.addWidget(self.list_widget)
widget = QtWidgets.QWidget()
widget.setLayout(vertical_layout)
self.setCentralWidget(widget)
self.show()
app = QtWidgets.QApplication(sys.argv)
ui = MainWindowUI()
sys.exit(app.exec_())
I see you have QListWidgetItem with you.
From documentation you can customize each widget item, customize it and add to your listwidget:
The appearance of the text can be customized with setFont(), setForeground(), and setBackground(). Text in list items can be aligned using the setTextAlignment() function. Tooltips, status tips and "What's This?" help can be added to list items with setToolTip(), setStatusTip(), an
d setWhatsThis().
http://doc.qt.io/qt-5/qlistwidgetitem.html#details
I have two QTreeWidgets in my QT app, using python (PyQt4).
I want to
Manually expand TreeWidget 1 to an item and then.
If this item is in TreeWidget 2, make TreeWidget 2 expand to the same item.
The reason is I have 2 tabs each have a treewidget.
You will need to excuse me, I'm not an experienced programmer and have been struggling.
Thanks
The question is a little light on details, since it doesn't specify what counts as the "same" item, and doesn't state which items can be expanded.
However, this simple demo script should provide a reasonable starting point:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.tree1 = QtGui.QTreeWidget(self)
self.tree2 = QtGui.QTreeWidget(self)
layout = QtGui.QHBoxLayout(self)
for tree in (self.tree1, self.tree2):
tree.header().hide()
tree.itemExpanded.connect(self.handleExpanded)
tree.itemCollapsed.connect(self.handleCollapsed)
for text in 'one two three four'.split():
item = QtGui.QTreeWidgetItem(tree, [text])
for text in 'red blue green'.split():
child = QtGui.QTreeWidgetItem(item, [text])
layout.addWidget(tree)
def handleExpanded(self, item):
self.syncExpansion(item, True)
def handleCollapsed(self, item):
self.syncExpansion(item, False)
def syncExpansion(self, item, expand=True):
if item is not None:
tree = item.treeWidget()
if tree is self.tree1:
tree = self.tree2
else:
tree = self.tree1
text = item.text(0)
for other in tree.findItems(text, QtCore.Qt.MatchFixedString):
other.setExpanded(expand)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(300, 500, 300, 300)
window.show()
sys.exit(app.exec_())