gpt4 book ai didi

python - 使用 QDataWidgetMapper 立即更改模型

转载 作者:行者123 更新时间:2023-12-04 09:20:55 25 4
gpt4 key购买 nike

我有一个 QTableView,它有一列带有 True/False 值。要更改此列中的值,我使用 QDataWidgetMapper 复选框。
我希望在单击复选框后立即更改值。通常在复选框失去焦点后进行更改(换句话说,您需要在应用程序的其他地方进行额外的点击)。
我试图这样做:checkBox.stateChanged.connect(lambda: data_mapper.submit()),但我不喜欢使用这种方法发出太多信号:首先它发出所有映射项都已更改的信号, 并且在复选框失去焦点后 - 仅更改一项的信号。
因此,我们的目标是在单击复选框后立即更新模型,并且只发出一个信号。
代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Mainwindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()

self.table = QtWidgets.QTableView()
self.widget_1 = Widget_1(self)

self.layout_1 = QtWidgets.QHBoxLayout()
self.layout_1.addWidget(self.table)
self.layout_1.addWidget(self.widget_1)

self.setLayout(self.layout_1)

headers = ['State', 'Prorerty_1', 'Prorerty_2']
data = [
[True, '1', '2'],
[False, '3', '4']
]

self.model = ListModel(data, headers)
self.table.setModel(self.model)
self.table.setSelectionBehavior(self.table.SelectRows)

self.table.clicked.connect(self.set_data_mapper)

self.model.dataChanged.connect(lambda value: print(value.row(), value.column(), value.data()))

def set_data_mapper(self):
position = self.table.selectionModel().selectedIndexes()[0].row()

self.widget_1.data_mapper.setModel(self.model)
self.widget_1.data_mapper.addMapping(self.widget_1.checkBox, 0)
self.widget_1.data_mapper.addMapping(self.widget_1.lineEdit, 1)
self.widget_1.data_mapper.setCurrentIndex(position)

self.widget_1.checkBox.stateChanged.connect(lambda: self.widget_1.data_mapper.submit())

class Widget_1(QtWidgets.QWidget):

def __init__(self, parent):
super().__init__()
self.parent = parent

self.layout_1 = QtWidgets.QGridLayout()
self.state = QtWidgets.QLabel('State')
self.checkBox = QtWidgets.QCheckBox()
self.property = QtWidgets.QLabel('Prop.1')
self.lineEdit = QtWidgets.QLineEdit()

self.layout_1.addWidget(self.state, 0, 0)
self.layout_1.addWidget(self.checkBox, 0, 1)
self.layout_1.addWidget(self.property, 1, 0)
self.layout_1.addWidget(self.lineEdit, 1, 1)

self.setLayout(self.layout_1)

self.data_mapper = QtWidgets.QDataWidgetMapper()

class ListModel(QtCore.QAbstractTableModel):

def __init__(self, data_list = [[]], headers = [], parent = None):
super(ListModel, self).__init__()
self.data_list = data_list
self.headers = headers

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

def columnCount(self, parent):
return len(self.headers)

def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.data_list[row][column]
return value

if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
value = self.data_list[row][column]
return value

if role == QtCore.Qt.FontRole:
if index.column() == 0:
boldfont = QtGui.QFont()
boldfont.setBold(True)
return boldfont

def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
self.data_list[row][column] = value
self.dataChanged.emit(index, index)
return True
return False

def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.headers[section]

if __name__ == '__main__':
app = QtWidgets.QApplication([])
application = Mainwindow()
application.show()


sys.exit(app.exec())

最佳答案

默认情况下,QDataWidgetMapper 策略是“AutoSubmit”,它会在任何小部件失去焦点时更新模型,因此如果您希望它在失去焦点时不发出,则必须使用“ManualSubmit”策略。另一方面,连接必须只完成一次,因为如果你这样做 n 次,那么将发出 n 个信号,在你的情况下,“每次按下按钮时都会调用 set_data_mapper。

class Mainwindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()

self.table = QtWidgets.QTableView(
selectionBehavior=QtWidgets.QAbstractItemView.SelectRows
)
self.widget_1 = Widget_1(self)

lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(self.table)
lay.addWidget(self.widget_1)

headers = ["State", "Prorerty_1", "Prorerty_2"]
data = [[True, "1", "2"], [False, "3", "4"]]

self.model = ListModel(data, headers)
self.table.setModel(self.model)
self.table.setSelectionBehavior(self.table.SelectRows)

self.widget_1.data_mapper.setModel(self.model)
self.widget_1.data_mapper.addMapping(self.widget_1.checkBox, 0)
self.widget_1.data_mapper.addMapping(self.widget_1.lineEdit, 1)

self.widget_1.data_mapper.setSubmitPolicy(
QtWidgets.QDataWidgetMapper.ManualSubmit
)

self.widget_1.checkBox.stateChanged.connect(self.widget_1.data_mapper.submit)
self.widget_1.lineEdit.textChanged.connect(self.widget_1.data_mapper.submit)

self.table.clicked.connect(self.update_current_index)

self.model.dataChanged.connect(
lambda value: print(value.row(), value.column(), value.data())
)

def update_current_index(self, index):
position = index.row()
self.widget_1.data_mapper.setCurrentIndex(position)

关于python - 使用 QDataWidgetMapper 立即更改模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63103041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com