0

So basically I have made a custom widget to be populated in QListWidget using this method: https://stackoverflow.com/a/49272941/7020404

I am working on this project: Shopping Chart

Whenever I clicked the Add to Chart button, I want the button will be disabled (I have made it) and I will have this custom widget item will be in the QListWidget's chart. But When I click the Cabbage's Add to Cart button, I got the Pork in the cart instead of Cabbage.

Disabled Button

The cart: enter image description here

I have 3 Tabs which are Food, Drink, and Chart. Each of the tabs has a class named TabFood, TabDrink, and TabCart.

Basically, I already have foodMenuList (A list of MenuWidget).

My MenuWidget Class:

class MenuWidget(QWidget):
def __init__(self, id, image, name, price, availability, sellerID):
    super().__init__()

    self.id = id
    self.image = image
    self.name = name
    self.price = price
    self.availability = availability
    self.sellerID = sellerID

    self.imageLabel = QLabel()
    self.imageLabel.setPixmap(self.image)

    self.nameLabel = QLabel()
    self.nameLabel.setText(self.name)

    self.priceLabel = QLabel()
    self.priceLabel.setText("Rp " + str(price))

    self.availabilityLabel = QLabel()
    if self.availability:
        self.availabilityLabel.setText("Ready")
    else:
        self.availabilityLabel.setText("Out of Stock")

    self.vbox = QVBoxLayout()
    self.vbox.addWidget(self.nameLabel)
    self.vbox.addWidget(self.availabilityLabel)
    self.vbox.addWidget(self.priceLabel)

    self.hbox = QHBoxLayout()
    self.addToCartButton = QPushButton("Add To Cart")

    self.hbox.addWidget(self.imageLabel)
    self.hbox.addLayout(self.vbox)
    self.hbox.addWidget(self.addToCartButton)

    self.setLayout(self.hbox)

This my TabFood Code:

class TabFood (QWidget):
def __init__(self, tabCart):
    super().__init__()

    self.hbox = QHBoxLayout()

    # Create the list
    self.mylist = QListWidget()

    self.mylist.show()
    self.hbox.addWidget(self.mylist)

    self.setLayout(self.hbox)

    for food in foodMenuList:
        # Add to list a new item (item is simply an entry in your list)
        item = QListWidgetItem(self.mylist)
        self.mylist.addItem(item)

        # Instanciate a custom widget
        food.addToCartButton.clicked.connect(lambda: self.addToCart(tabCart, food)) ##tabCart is the instance of TabCart class (Just like a TabFood Class)
        item.setSizeHint(food.minimumSizeHint())

        # # Associate the custom widget to the list entry
        self.mylist.setItemWidget(item, food)


def addToCart(self, tabcart, item):
    self.sender().setEnabled(False)
    print(item.name) ##This always return pork instead of cabbage
    itemNew = OrderWidget(item.id, item.image, item.name, item.price, item.availability, item.sellerID)
    tabcart.addItem(itemNew) ##It is only a function to add item in tabcart's QListWidget

I think the problem is in this line:

food.addToCartButton.clicked.connect(lambda: self.addToCart(tabCart, food))

Because in the addToChart function, when I print the item.name , it always prints pork instead of cabbage. But I am not sure because I have provided the food as the parameter in the iteration to determine which item has been selected.

So How Can I fix this problem?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

0 Answers0