Reputation: 19369
This code creates QTreeWidget
with QComboBox
and QLineEdit
set as the item widgets
.
If to follow these steps:
Right after this dialog opens...
Click the item (not its item-widget). Item's background color turns blue.
Now click any item-widget of another item (Combobox or LineEdit).
The item the combobox is assigned to as the item-widget gets selected (turns blue). Any other attempts to select the item by clicking its item-widget fail. You would have to close dialog and re-open it again. Since this happens only once.
Is it possible to make this feature persistent... so every time an item-widget is clicked the item holding it gets selected as well making a full impression the item and its item-widgets is the same thing. As it acts by default I have to click the item itself to make it selected.
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Tree(QtGui.QTreeWidget):
def __init__(self, *args, **kwargs):
super(Tree, self).__init__()
for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
item=QtGui.QTreeWidgetItem()
item.setText(0, each)
self.addTopLevelItem(item)
self.setItemWidget(item, 1, QtGui.QComboBox())
self.setItemWidget(item, 2, QtGui.QLineEdit())
self.setColumnCount(5)
self.resize(360,240)
self.show()
tree=Tree()
sys.exit(app.exec_())
Upvotes: 1
Views: 2415
Reputation: 3460
Use have to use mousePressEvent
(again) to implemented when item in widget has Press;
.
.
.
comboBox = QtGui.QComboBox()
lineEdit = QtGui.QLineEdit()
self.setItemWidget(item, 1, comboBox)
self.setItemWidget(item, 2, lineEdit)
# Set new event
comboBox.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = comboBox)
lineEdit.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = lineEdit)
.
.
.
def mousePressEventChild (self, QMouseEvent, currentQTreeWidgetItem = None, child = None):
if isinstance(currentQTreeWidgetItem, QtGui.QTreeWidgetItem): # <- Check this widget has QTreeWidgetItem.
self.setCurrentItem(currentQTreeWidgetItem) # <- set current index by QTreeWidgetItem.
super(child.__class__, child).mousePressEvent(QMouseEvent) # <- Default old mouse press event.
Full example code;
import sys
from functools import partial
from PyQt4 import QtCore, QtGui
class Tree(QtGui.QTreeWidget):
def __init__(self, *args, **kwargs):
super(Tree, self).__init__()
for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
item=QtGui.QTreeWidgetItem([each])
self.addTopLevelItem(item)
comboBox = QtGui.QComboBox()
lineEdit = QtGui.QLineEdit()
self.setItemWidget(item, 1, comboBox)
self.setItemWidget(item, 2, lineEdit)
comboBox.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = comboBox)
lineEdit.mousePressEvent = partial(self.mousePressEventChild, currentQTreeWidgetItem = item, child = lineEdit)
self.setColumnCount(5)
self.resize(360,240)
self.show()
def mousePressEventChild (self, QMouseEvent, currentQTreeWidgetItem = None, child = None):
if isinstance(currentQTreeWidgetItem, QtGui.QTreeWidgetItem):
self.setCurrentItem(currentQTreeWidgetItem)
super(child.__class__, child).mousePressEvent(QMouseEvent)
app = QtGui.QApplication([])
tree=Tree()
sys.exit(app.exec_())
Click event Reference : http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#mousePressEvent
Regards,
Upvotes: 2