How to refresh the item which i've added into ItemWidget , and how to get the currentItem data?

94 views Asked by At

I guess it's better to paste all the code :

class Edge_Conn(QMainWindow):
    def __init__(self):
        super(Edge_Conn, self).__init__()
        loadUi('untitled.ui', self)
        self.Ui_init()
        self.Data_init()

    def Ui_init(self):
        self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget.setGeometry(QtCore.QRect(50, 20, 581, 451))
        self.listWidget.lower()
        self.pushButton.clicked.connect(self.refreshWidget)
        self.setStyleSheet("QListWidget{border:1px solid gray; color:black; }"
                       "QListWidget::Item:hover{background:rgba(62,166,117,12); }"
                       "QListWidget::item:selected:!active{border-width:0px; background:white; }"
                       "QListWidget::item:selected:active{border-width:0px; background:rgba(62,166,117,12); }"
                       )
        self.listWidget.itemClicked.connect(self.show_select_item)

    def Data_init(self):
        self.datas = [
            {'animal': 'cat', 'sound': 'Meow'},
            {'animal': 'dog', 'sound': 'Woof'},
        ]
        for data in self.datas:
            item = QListWidgetItem()
            item.setSizeHint(QSize(480, 80))
            widget = self.get_item_widget(data)
            self.listWidget.addItem(item)
            self.listWidget.setItemWidget(item, widget)

    def show_select_item(self):
        itemNumber = self.listWidget.currentRow()  # +++
        current_item = self.datas[itemNumber]
        aaa = self.listWidget.currentItem().data(itemNumber )
        print(itemNumber)  #0 or 1
        print(current_item) # {'animal': 'cat', 'sound': 'Meow'},
        print(aaa)   # None here

    def get_item_widget(self,data):
        animal = data["animal"]
        sound = data["sound"]
        liswidget = QWidget()
        layout_main = QHBoxLayout()
        map_label = QLabel()
        map_label.setFixedSize(60, 60)
        photo = 'photo_3x.png'
        maps = QPixmap(photo).scaled(50, 50)
        map_label.setPixmap(maps)
        layout_right = QVBoxLayout()

        layout_main.addWidget(map_label)
        layout_main.addLayout(layout_right)
        layout_right.addWidget(QLabel(animal))
        layout_right.addWidget(QLabel(sound))
        liswidget.setLayout(layout_main)
        return liswidget
    def refreshWidget(self):
        # how to code here if i wanner update cat or dog?
        pass

app = QApplication(sys.argv)
mainwindow = Edge_Conn()
mainwindow.show()
sys.exit(app.exec_())
            

I want to get the data of currentItem if i click it. You can see that i can get the currentRow if click the listWidgetItem,but i can not print any data . I guess it's not a good idea through data[index] because the data is not large enough now . So , what should I do?

0

There are 0 answers