我已经发布过此程序,我不想重复重复的问题 但是在此我将小部件添加到子小部件中,在我的所有功能正常工作之前,当我添加小部件时,我在该程序中遇到了一些问题,我尝试了不同的方式。但是仍然存在相同的问题。在子小部件中我添加了一行编辑我想更改一行编辑文本,然后它会自动更改最后一行的编辑文本。搜索功能现在也无法使用。请有人帮我解决此程序
下面是我的代码:
from PyQt4 import QtCore, QtGui
from functools import partial
global qty
class InventoryDelegate(QtGui.QStyledItemDelegate):
def paint(self, painter, option, index):
super(InventoryDelegate, self).paint(painter, option, index)
if not index.parent().isValid():
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor("white")))
r = QtCore.QRect(option.rect)
r.adjust(0, 1, 0, -1)
painter.drawLine(r.topLeft(), r.topRight())
painter.drawLine(r.bottomLeft(), r.bottomRight())
painter.restore()
def sizeHint(self, option, index):
s = super(InventoryDelegate, self).sizeHint(option, index)
s.setHeight(55)
return s
class Inventory_Items(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Inventory_Items, self).__init__(parent)
inv_obj =[{u'productPrice': 123.0, u'productSerialId': u'123458', u'productName': u'Renewal Application Form', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/1546958016_35_admin_index.jpeg', u'productUnit': u'Quantity', u'data': [{u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123457', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 65, u'productVariant__sku': None, u'quantity': -11.0}, {u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123458', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 67, u'productVariant__sku': None, u'quantity': 10.0}, {u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123459', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 69, u'productVariant__sku': None, u'quantity': 30.0}], u'productPk': 1837}, {u'productPrice': 1760.0, u'productSerialId': u'1168', u'productName': u'Truth or Delusion', u'productDiscount': 5, u'productdp': u'/media/ecommerce/pictureUploads/1546959987_35_admin_default.png', u'productUnit': u'Kilogram', u'data': [{u'product': 2180, u'product__unit': u'Kilogram', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-BOK-D-2016-TRUDEL-02', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1760.0, u'pk': 70, u'productVariant__sku': None, u'quantity': 10.0}, {u'product': 2180, u'product__unit': u'Kilogram', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-BOK-D-2016-TRUDEL-01', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1760.0, u'pk': 71, u'productVariant__sku': None, u'quantity': 1.0}], u'productPk': 2180}, {u'productPrice': 240.0, u'productSerialId': u'1219', u'productName': u'One-to-One Follow Up Slips ( Qty 10 pads - 50 sheets per pad)', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/https%3A//bnistore.in/wp-content/uploads/2016/02/323.jpg', u'productUnit': u'Quantity', u'data': [{u'product': 2185, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123457', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 240.0, u'pk': 62, u'productVariant__sku': None, u'quantity': -48.0}], u'productPk': 2185}, {u'productPrice': 1500.0, u'productSerialId': u'3293', u'productName': u'Membership Application Form (Package of 10)', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/https%3A//bnistore.in/wp-content/uploads/2017/08/New-Members-Application-Form.png', u'productUnit': u'quantity', u'data': [{u'product': 2225, u'product__unit': u'quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-PRT-D-2016-NEWAPP-1', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1500.0, u'pk': 66, u'productVariant__sku': None, u'quantity': 81.0}], u'productPk': 2225}]
w = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(w)
hbox = QtGui.QHBoxLayout()
self.le = QtGui.QLineEdit()
search = QtGui.QPushButton("search")
search.clicked.connect(self.search_item)
hbox.addWidget(self.le)
hbox.addWidget(search)
vbox.addLayout(hbox)
self.inventory_widget = QtGui.QTreeWidget(columnCount=7,
expandsOnDoubleClick=False,
indentation=0,
focusPolicy=QtCore.Qt.NoFocus,
iconSize=QtCore.QSize(40,60))
vbox.addWidget(self.inventory_widget)
self.inventory_widget.setColumnWidth(2, 350)
self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
header = self.inventory_widget.header()
header.hide()
header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.Stretch)
header.setResizeMode(3, QtGui.QHeaderView.Stretch)
header.setResizeMode(4, QtGui.QHeaderView.Stretch)
header.setResizeMode(5, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(6, QtGui.QHeaderView.ResizeToContents)
delegate = InventoryDelegate(self.inventory_widget)
self.inventory_widget.setItemDelegate(delegate)
c=[]
for obj in inv_obj:
it = QtGui.QTreeWidgetItem()
self.inventory_widget.addTopLevelItem(it)
values = [
obj["productName"],
"Unit : {0} ".format(obj["productUnit"]),
"Price : {0} ".format(obj["productPrice"]),
"Discount : {0} ".format(obj["productDiscount"])
]
for i, t in enumerate(values):
it.setText(i + 1, t)
for v in obj['data']:
child_it = QtGui.QTreeWidgetItem()
it.addChild(child_it)
value = [
v['product__serialNo'],
"{}".format(v['product__howMuch']),
"{}".format(v['quantity']),
]
for l, m in enumerate(value):
child_it.setText(l + 1, m)
widget = QtGui.QWidget(self) # here i am adding new widget to child widget
button_save = QtGui.QPushButton("save")
button_save.clicked.connect(self.save_qty)
self.le= QtGui.QLineEdit()
self.le.setFocus()
self.le.textChanged.connect(self.doSomething)
self.le.setText(m)
text = self.le.text()
button_save.setStyleSheet("background-color: blue;color: white;")
button_delete = QtGui.QPushButton("delete")
button_delete.setStyleSheet("background-color: red;color: white;")
lay = QtGui.QHBoxLayout(widget)
lay.addWidget(self.le)
lay.addStretch()
lay.addWidget(button_save)
lay.addWidget(button_delete)
self.inventory_widget.setItemWidget(child_it, 3, widget)
self.inventory_widget.expandAll()
self.setCentralWidget(w)
def search_item(self):
filter_text = self.le.text()
is_valid_filter = filter_text == ""
for i in range(self.inventory_widget.topLevelItemCount()):
top_item = self.inventory_widget.topLevelItem(i)
flag = (str(filter_text).lower() in str(top_item.text(1)).lower()) or is_valid_filter
top_item.setHidden(not flag) #before its working good but after added widget to child widget search functionality is not working
def doSomething(self,text):
global qty
print text,"TEXTTT"
qty = text
def save_qty(self):
global qty
print qty,"qtyyy"
self.le.setText(qty) #here i click save buttons one more line edit text also changing...
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
settingobj= Inventory_Items()
settingobj.showFullScreen()
sys.exit(app.exec_())
答案 0 :(得分:2)
问题是您多次覆盖一个变量,例如在开始时self.le引用执行过滤的QLineEdit,但是self.le将是QTreeWidget中设置的小部件,因此通常self .le不再是初始QLineEdit,因此筛选器失败。
一种解决方案是创建一个自定义窗口小部件,用于处理该信息并显示单击的信号和文本,在使用sender方法获取窗口小部件的插槽中
最后避免使用全局变量,有关更多参考,请阅读Why are global variables evil?。
from PyQt4 import QtCore, QtGui
class InventoryDelegate(QtGui.QStyledItemDelegate):
def paint(self, painter, option, index):
super(InventoryDelegate, self).paint(painter, option, index)
if not index.parent().isValid():
painter.save()
painter.setPen(QtGui.QPen(QtGui.QColor("white")))
r = QtCore.QRect(option.rect)
r.adjust(0, 1, 0, -1)
painter.drawLine(r.topLeft(), r.topRight())
painter.drawLine(r.bottomLeft(), r.bottomRight())
painter.restore()
def sizeHint(self, option, index):
s = super(InventoryDelegate, self).sizeHint(option, index)
s.setHeight(55)
return s
class Widget(QtGui.QWidget):
saveClicked = QtCore.pyqtSignal()
deleteClicked = QtCore.pyqtSignal()
def __init__(self, text, parent=None):
super(Widget, self).__init__(parent)
self.le= QtGui.QLineEdit(text=text)
button_save = QtGui.QPushButton(text="save", clicked=self.saveClicked)
button_save.setStyleSheet("background-color: blue;color: white;")
button_delete = QtGui.QPushButton(text="delete", clicked=self.deleteClicked)
button_delete.setStyleSheet("background-color: red;color: white;")
lay = QtGui.QHBoxLayout(self)
lay.addWidget(self.le)
lay.addStretch()
lay.addWidget(button_save)
lay.addWidget(button_delete)
def text(self):
return self.le.text()
class Inventory_Items(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Inventory_Items, self).__init__(parent)
inv_obj =[{u'productPrice': 123.0, u'productSerialId': u'123458', u'productName': u'Renewal Application Form', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/1546958016_35_admin_index.jpeg', u'productUnit': u'Quantity', u'data': [{u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123457', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 65, u'productVariant__sku': None, u'quantity': -11.0}, {u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123458', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 67, u'productVariant__sku': None, u'quantity': 10.0}, {u'product': 1837, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123459', u'product__howMuch': 2.0, u'productVariant__serialId': None, u'product__price': 123.0, u'pk': 69, u'productVariant__sku': None, u'quantity': 30.0}], u'productPk': 1837}, {u'productPrice': 1760.0, u'productSerialId': u'1168', u'productName': u'Truth or Delusion', u'productDiscount': 5, u'productdp': u'/media/ecommerce/pictureUploads/1546959987_35_admin_default.png', u'productUnit': u'Kilogram', u'data': [{u'product': 2180, u'product__unit': u'Kilogram', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-BOK-D-2016-TRUDEL-02', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1760.0, u'pk': 70, u'productVariant__sku': None, u'quantity': 10.0}, {u'product': 2180, u'product__unit': u'Kilogram', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-BOK-D-2016-TRUDEL-01', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1760.0, u'pk': 71, u'productVariant__sku': None, u'quantity': 1.0}], u'productPk': 2180}, {u'productPrice': 240.0, u'productSerialId': u'1219', u'productName': u'One-to-One Follow Up Slips ( Qty 10 pads - 50 sheets per pad)', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/https%3A//bnistore.in/wp-content/uploads/2016/02/323.jpg', u'productUnit': u'Quantity', u'data': [{u'product': 2185, u'product__unit': u'Quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'123457', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 240.0, u'pk': 62, u'productVariant__sku': None, u'quantity': -48.0}], u'productPk': 2185}, {u'productPrice': 1500.0, u'productSerialId': u'3293', u'productName': u'Membership Application Form (Package of 10)', u'productDiscount': 0, u'productdp': u'/media/ecommerce/pictureUploads/https%3A//bnistore.in/wp-content/uploads/2017/08/New-Members-Application-Form.png', u'productUnit': u'quantity', u'data': [{u'product': 2225, u'product__unit': u'quantity', u'productVariant__unitPerpack': None, u'productVariant': None, u'productVariant__price': None, u'product__serialNo': u'IND-PRT-D-2016-NEWAPP-1', u'product__howMuch': 1.0, u'productVariant__serialId': None, u'product__price': 1500.0, u'pk': 66, u'productVariant__sku': None, u'quantity': 81.0}], u'productPk': 2225}]
w = QtGui.QWidget()
vbox = QtGui.QVBoxLayout(w)
hbox = QtGui.QHBoxLayout()
self.le = QtGui.QLineEdit()
search = QtGui.QPushButton("search")
search.clicked.connect(self.search_item)
hbox.addWidget(self.le)
hbox.addWidget(search)
vbox.addLayout(hbox)
self.inventory_widget = QtGui.QTreeWidget(columnCount=7,
expandsOnDoubleClick=False,
indentation=0,
focusPolicy=QtCore.Qt.NoFocus,
iconSize=QtCore.QSize(40,60))
vbox.addWidget(self.inventory_widget)
self.inventory_widget.setColumnWidth(2, 350)
self.inventory_widget.setStyleSheet("QWidget {background-color: black;color: white;}")
header = self.inventory_widget.header()
header.hide()
header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.Stretch)
header.setResizeMode(3, QtGui.QHeaderView.Stretch)
header.setResizeMode(4, QtGui.QHeaderView.Stretch)
header.setResizeMode(5, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(6, QtGui.QHeaderView.ResizeToContents)
delegate = InventoryDelegate(self.inventory_widget)
self.inventory_widget.setItemDelegate(delegate)
c=[]
for obj in inv_obj:
it = QtGui.QTreeWidgetItem()
self.inventory_widget.addTopLevelItem(it)
values = [
obj["productName"],
"Unit : {0} ".format(obj["productUnit"]),
"Price : {0} ".format(obj["productPrice"]),
"Discount : {0} ".format(obj["productDiscount"])
]
for i, t in enumerate(values):
it.setText(i + 1, t)
for v in obj['data']:
child_it = QtGui.QTreeWidgetItem()
it.addChild(child_it)
value = [
v['product__serialNo'],
"{}".format(v['product__howMuch']),
"{}".format(v['quantity']),
]
for l, m in enumerate(value):
child_it.setText(l + 1, m)
widget = Widget(m)
widget.saveClicked.connect(self.save_qty)
self.inventory_widget.setItemWidget(child_it, 3, widget)
self.inventory_widget.expandAll()
self.setCentralWidget(w)
@QtCore.pyqtSlot()
def search_item(self):
filter_text = self.le.text()
is_valid_filter = filter_text == ""
for i in range(self.inventory_widget.topLevelItemCount()):
top_item = self.inventory_widget.topLevelItem(i)
flag = (str(filter_text).lower() in str(top_item.text(1)).lower()) or is_valid_filter
top_item.setHidden(not flag)
@QtCore.pyqtSlot()
def save_qty(self):
widget = self.sender()
print(widget.text())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
settingobj= Inventory_Items()
settingobj.showFullScreen()
sys.exit(app.exec_())