PyQT:在QListWidgetItem中获取内容CustFormWidgetIem

时间:2018-12-10 17:07:31

标签: python pyqt pyqt5 qlistwidget qlistwidgetitem

我有一个CustomFormWidgetItem,我添加到了QListWidgetItem

在主窗口小部件中,我正在尝试通过{p>来查询CustomFormWidgetItem上的值

self.listWidget.currentItemChanged.connect(self.print_info)  # QListWidget

def print_info(self):
    print(self.listWidget.currentItem())
    print(self.listWidget.row(self.ui.catalog_list_wid.currentItem()))

当我单击列表中的任何项目时,

<PyQt5.QtWidgets.QListWidgetItem object at 0x00000209A3831E58>
0  #  This is the row

我想要QListWidgetItem中的实际物品,如何获取该物品?

编辑(添加MVE):

class Roles:
    IdRole = QtCore.Qt.UserRole + 1000
    NameRole = QtCore.Qt.UserRole + 1001
    VersionRole = QtCore.Qt.UserRole + 1002
    InstalledRole = QtCore.Qt.UserRole + 1003

class ParentWid(QtWidgets.QDialog, Ui_ParentWidget):
    def __init__(self, data={}, parent=None):
        super(ParentWid, self).__init__(parent)
        self.data = data
        self.setupUi(self)
        self.set_widget_data()

    def set_widget_data(self):
        for item in self.data:
            lst_item = QtWidgets.QListWidgetItem()
            self.listWidget.addItem(lst_item)
            custFormItem = CustomFormWidget(item, lst_item)
            lst_item.setSizeHint(custFormItem.sizeHint())

    def print_results(self):
        v = (("id", Roles.IdRole), ("name", Roles.NameRole), ("version", Roles.VersionRole), ("installed", Roles.InstalledRole),)
        results = []
        for i in range(self.listWidget.count()):
            it = self.listWidget.item(i)
            d = {}
            for k, r in v:
                d[k] = it.data(r)
            results.append(d)
        print(results)

    # test
    def closeEvent(self, event):
        self.print_results()
        super(ParentWid, self).closeEvent(event)

class CustomFormWidget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, data, item, parent=None):
        super(CustomFormWidget, self).__init__(parent)
        self._item = item
        self._item.listWidget().setItemWidget(self._item, self)
        self.setupUi(self)
        v = (("id", Roles.IdRole), ("name", Roles.NameRole), ("version", Roles.VersionRole), ("installed", Roles.InstalledRole),)
        for k, r in v:
            self._item.setData(r, data[k])
        self.update_view()
        self.install_btn.clicked.connect(self.on_click)

    def update_view(self):
        self.pkg_name.setText(self._item.data(Roles.NameRole))
        self.pkg_version.setText(self._item.data(Roles.VersionRole))
        v = self._item.data(Roles.InstalledRole)
        self.install_btn.setText("Installed" if v else "Not Installed")
        self.install_btn.setEnabled(not v)

    @QtCore.pyqtSlot()
    def on_click(self):
        self._item.setData(Roles.InstalledRole, not self._item.data(Roles.InstalledRole))
        self.update_view()

        v = (("id", Roles.IdRole), ("name", Roles.NameRole), ("version", Roles.VersionRole), ("installed", Roles.InstalledRole),)
        for k, r in v:
            print(k, self._item.data(r))


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)

    data = [
        {
            'id': 1,
            'name': 'pkg-foo',
            'version': '0.1',
            'installed': False
        },
        {
            'id': 2,
            'name': 'pkg-bar',
            'version': '0.1',
            'installed': False
        }
    ]
    w = ParentWid(data)
    w.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

currentItemChanged信号发送当前和先前的currentItem,因此我们可以直接使用第一个数据。另一方面,正如OP所指出的,它的代码基于我的previous answer,其中指出在这种情况下,最好将数据保存在QListWidget中,因为它具有内部模型,因此在这种情况下,我们可以利用它来获取数据:

  .div-img img {
  position: absolute;
  top: 0;
  left: 50%;
  display: block;
  transform: translateX(-50%);
  opacity: 0;
  animation-duration: calc(var(--time) * 1s);
  animation-iteration-count: infinite;
  animation-name: fade;
}

.div-img img:nth-child(1) {
  animation-delay: 0s;
}

.div-img img:nth-child(2) {
  animation-delay: calc(var(--time) / 8 * 1s);
}

.div-img img:nth-child(3) {
  animation-delay: calc(var(--time) / 4 * 1s);
}

.div-img img:nth-child(4) {
  animation-delay: calc(var(--time) / 2.66 * 1s);
}

.div-img img:nth-child(5) {
  animation-delay: calc(var(--time) / 2 * 1s);
}

.div-img img:nth-child(6) {
  animation-delay: calc(var(--time) / 1.6 * 1s);
}

.div-img img:nth-child(7) {
  animation-delay: calc(var(--time) / 1.33 * 1s);
}

.div-img img:nth-child(8) {
  animation-delay: calc(var(--time) / 1.14 * 1s);
}

.div-txt {
  animation-duration: calc(var(--time) * 1s);
  animation-iteration-count: infinite;
  animation-name: color-change;
  text-align: right;
}

@keyframes color-change {
  0%,
  25%,
  100% {
    background-color: #fff;
    box-shadow: 0 0 0 rgba(0, 0, 0, 0.1);
  }
  1%,
  24% {
    box-shadow: 0 10px 90px rgba(0, 0, 0, 0.4);
  }
}

@keyframes fade {
  0%,
  20%,
  100% {
    opacity: 0;
    z-index: auto;
  }
  1%,
  99% {
    z-index: 1;
  }
  8%,
  12% {
    opacity: 1;
  }
}

@media all and (min-width: 1170px) {
  .div-wrap {
    flex-flow: row nowrap;
    justify-content: space-around;
  }