Python QAbstractItemModel.createIndex方法代码示例

本文展示了如何在 PyQt5 中使用 QStringListModel 的 createIndex 方法来更新列表视图中的数据。OrderItemsDialog 类是一个对话框,用于更改定性参数值的顺序。当用户点击上下按钮时,选中的值与其前一个或后一个值互换。replaceItems 方法实现了这个功能,通过调用 createIndex 更新当前选中索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python QStringListModel.createIndex方法代码示例

本文整理汇总了Python中PyQt5.QtCore.QStringListModel.createIndex方法的典型用法代码示例。如果您正苦于以下问题:Python QStringListModel.createIndex方法的具体用法?Python QStringListModel.createIndex怎么用?Python QStringListModel.createIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.QtCore.QStringListModel的用法示例。

示例1: OrderItemsDialog

# 需要导入模块: from PyQt5.QtCore import QStringListModel [as 别名]
# 或者: from PyQt5.QtCore.QStringListModel import createIndex [as 别名]
class OrderItemsDialog(QDialog, Ui_OrderItemsDialog):
    """
    This class is a dialog for changing the order of qualitative parameter's values

    The dialog lists the values and allow changing the order using Up and Down buttons
    which replace the selected value with the one before/after it in the list
    """

    def __init__(self, parent, listToOrder: list):
        """
        Initialize the change order dialog

        Args:
            parent : (QDialog) - The parent dialog
            listToOrder : (list) - The list to set the order for
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.listToOrder = listToOrder
        self.model = QStringListModel(self)
        self.model.setStringList(listToOrder)
        self.listView_items.setModel(self.model)
        self.pushButton_up.clicked.connect(self.pushButton_up_clicked)
        self.pushButton_down.clicked.connect(self.pushButton_down_clicked)

    def pushButton_up_clicked(self):
        """
        Replace the selected item with the one before it
        """
        selectedIndex = self.listView_items.currentIndex().row()
        self.replaceItems(selectedIndex, selectedIndex - 1)

    def pushButton_down_clicked(self):
        """
        Replace the selected item with the one after it
        """
        selectedIndex = self.listView_items.currentIndex().row()
        self.replaceItems(selectedIndex, selectedIndex + 1)

    def replaceItems(self, fromIndex, toIndex):
        """
        Replace 2 items

        Args:
            fromIndex   : (int) - the index of the selected item
            toIndex     : (int) - the target index of the selected item
        """
        if toIndex in range(len(self.listToOrder)):
            temp = self.listToOrder[toIndex]
            self.listToOrder[toIndex] = self.listToOrder[fromIndex]
            self.listToOrder[fromIndex] = temp
            self.model.setStringList(self.listToOrder)
            self.model.dataChanged.emit(
                self.listView_items.indexAt(QPoint(0, 0)),
                self.listView_items.indexAt(QPoint(len(self.listToOrder), 0)))
            index = self.model.createIndex(toIndex, 0)  # 创建index
            self.listView_items.setCurrentIndex(index)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M_qsqsqsq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值