如何添加“选择一个..."使用 QAbstractTableModel(模型/视图)时到 QComboBox? [英] How to add "Select one..." to QComboBox when using QAbstractTableModel (Model/View)?

查看:64
本文介绍了如何添加“选择一个..."使用 QAbstractTableModel(模型/视图)时到 QComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QAbstractTableModel 来填充 QComboBox.这很好用,但我希望总是有第一个组合框索引来包含选择一个..."的值.

I'm using a QAbstractTableModel to populate a QComboBox. This works great, but I wish to always have the very first combobox index to contain a value of "Select one...".

这可能吗,如果可能,怎么办?

Is this possible, and if so - how?

我有一个 combobox,我将模型设置为:

I have a combobox, which I set a model to:

model = ProjectTableModel(projects)
combobox.setModel(model)

我的模型:

class ProjectTableModel(QtCore.QAbstractTableModel):

    def __init__(self, projects=[], parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._projects = projects

    def rowCount(self, parent):
        return len(self._projects)

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        row = index.row()
        column = index.column()

        if role == QtCore.Qt.DisplayRole and column == 0:
            # Set the item's text
            project = self._projects[row]
            name = project.name()
            return name
        elif role == QtCore.Qt.UserRole and column == 0:
            # Set the "itemData"
            project = self._projects[row]
            id = project.id()
            return id

推荐答案

您可以在获取/设置值时添加适当的条件,并在必要时调整行数/行数.下面的示例显示了如何执行此操作,但您应该仔细检查所有代码,以确保在访问 _projects 项时始终正确调整行.(并注意,在访问模型本身中的行时,您不需要调整行号).

You can add an appropriate condition when getting/setting values and adjust the row count/number where necessary. The example below shows how to do this, but you should check all your code carefully to make sure the row is always adjusted properly when accessing the _projects items. (And note that you don't need to to adjust the row number when accessing rows in the model itself).

class ProjectTableModel(QtCore.QAbstractTableModel):

    def __init__(self, projects=[], parent=None):
        QtCore.QAbstractTableModel.__init__(self, parent)
        self._projects = projects

    def rowCount(self, parent):
        return len(self._projects) + 1 # adjust row count

    def columnCount(self, parent):
        return 2

    def data(self, index, role):
        row = index.row() - 1 # adjust row number
        column = index.column()

        if role == QtCore.Qt.DisplayRole and column == 0:
            if row >= 0:
                # Set the item's text
                project = self._projects[row]
                return project.name()
            else:
                return 'Select one...'
        elif role == QtCore.Qt.UserRole and column == 0 and row >= 0:
            # Set the "itemData"
            project = self._projects[row]
            id = project.id()
            return id

    def setData(self, index, value, role):
        row = index.row() - 1  # adjust row number
        column = index.column()

        # ignore the first item in the model
        if role == QtCore.Qt.DisplayRole and column == 0 and row >= 0:
            project = self._projects[row]
            project.setName(value) # or whatever

这篇关于如何添加“选择一个..."使用 QAbstractTableModel(模型/视图)时到 QComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆