我如何实现更新qtableview中的多行 [英] how can i achieve to update multiple rows in a qtableview

查看:439
本文介绍了我如何实现更新qtableview中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的qtablemodel和qtableview.我想添加一个功能,用户可以选择多行并通过更改此行内的值之一来进行选择.他实际上将在所有行中更改此值. 例如用户在选择整个表格时可以将表格中所有人的姓名更改为alice.

I have a customized qtablemodel and a qtableview. I would like to add a feature that the user can select multiple rows and by changing one of the values within this rows. He would actually change this value in all rows. e.g. the user could change the name of all persons in the table to alice when he selected the whole table.

您能帮助我实现这一目标吗?

Can you help me to achieve this?

我不明白如何针对不同的行多次触发模型的setData.还是可以告诉我,在调用模型中的setData函数之前,qtableview发送给模型的信号是什么?

I do not understand how i can trigger the setData of the model a multiple times for different rows. Or can you tell me which signal the qtableview sends to the model before the setData function within the model is called?

非常感谢 唐尼

推荐答案

对于在同一列中同时编辑所有选定值的问题,我可能有一个更为直接的解决方案.与其覆盖QTableView :: edit(),不如覆盖QTableView :: commitData(editor),它在用户提交其编辑后便被调用.

I might have a slightly more straight-forward solution the problem of editing all selected values in the same column at the same time. Instead of overriding QTableView::edit(), it's easier to override QTableView::commitData(editor), which is called after the user has submitted their edit.

简而言之:当用户提交其编辑内容时,将遍历所有其他选定的行,并将与更改后的单元格完全相同的值更改应用于与已编辑的单元格相同的列.

In short: When the user submits their edits, iterate through all other selected rows and apply the exact same value change to cells with the same column as the edited cell.

这是一个Python示例,向C ++的转换应该很容易(在各处添加分号,将self替换为this):

Here's a Python example, translation to C++ should be easy (add semicolons everywhere, replace self with this):

class ImageTableView(QtGui.QTableView):
    def commitData(self, editor):
        # call parent commitData first
        super(ImageTableView, self).commitData(editor)

        # self.currentIndex() is the QModelIndex of the cell just edited
        theModel = self.currentIndex().model()
        # get the value that the user just submitted
        value = theModel.data(self.currentIndex(), QtCore.Qt.EditRole)

        curRow, curCol = self.currentIndex().row(), self.currentIndex().column()

        # selection is a list of QItemSelectionRange instances
        for isr in self.selectionModel().selection():
            rows = range(isr.top(), isr.bottom()+1)
            for row in rows:
                if row != curRow:
                    # row,curCol is also in the selection. make an index:
                    idx = theModel.index(row, curCol)
                    # so we can apply the same value change
                    theModel.setData(idx, value, QtCore.Qt.EditRole)

这篇关于我如何实现更新qtableview中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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