gpt4 book ai didi

python - 当 QListview 项的复选框更改时发出信号

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

当 qlistview 项目的复选框被更改时,我如何发出信号?理想情况下,发出的信号会有某种指向发生变化的项目的指针,这样我就可以在 PySide 中采取相应的行动。

enter image description here

import os
import sys
import json
from PySide import QtGui, QtCore


class MegaMergeWindow(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
super(MegaMergeWindow, self).__init__(*args, **kwargs)
self.TITLE = 'Mega Merge'
self.VERSION = '1.0.0'
self.setWindowTitle(self.TITLE + ' | ' + self.VERSION)
self.resize(350,500)

# vars
self.user_folder = os.path.join(os.getenv('LOCALAPPDATA'), 'MegaMerge')

# controls
self.ui_files = QtGui.QListView()
self.ui_files.setModel(QtGui.QStandardItemModel())
self.ui_files.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

self.ui_merge = QtGui.QPushButton('Merge')

main_layout = QtGui.QVBoxLayout()
main_layout.addWidget(self.ui_files)
main_layout.addWidget(self.ui_merge)

main_widget = QtGui.QWidget()
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)

# signals
self.ui_merge.clicked.connect(self.merge_clicked)
self.populate_files()


def populate_files(self, files=[], clear=False):
model = self.ui_files.model()

if clear:
model.clear()

files = ['Doug','Kevin','Amy','Melissa','John']

for f in files:
name = os.path.basename(f)
item = QtGui.QStandardItem(name)
item.setData(f, role=QtCore.Qt.UserRole)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Checked)
model.appendRow(item)
model.sort(0, QtCore.Qt.AscendingOrder)


def collect_paths(self):
files = []
model = self.ui_files.model()
for index in range(model.rowCount()):
item = model.item(index)
if item.checkState() == QtCore.Qt.Checked:
files.append(item.text())
return files


def merge_files(self, files=[]):
print files


def merge_clicked(self):
files = self.collect_paths()
print files


def main():
app = QtGui.QApplication(sys.argv)
ex = MegaMergeWindow()
ex.show()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

最佳答案

在 Qt4 中没有信号表明 QCheckBox 是否被选中 QAbstractItemView,只是在 Qt5 中 dataChanged 信号被修改为发送被修改的角色,因此区分检查是否被更改。

在 Qt4 中,有几种方法可以创建该信号,一种方法是使用委托(delegate)来跟踪该角色的变化,如下所示:

class StyledItemDelegate(QtGui.QStyledItemDelegate):
checked = QtCore.Signal(QtCore.QModelIndex, int)
def editorEvent(self, event, model, option, index):
if model.flags(index) & QtCore.Qt.ItemIsUserCheckable:
# before the change
last_value = index.data(QtCore.Qt.CheckStateRole)
value = QtGui.QStyledItemDelegate.editorEvent(self, event, model, option, index)
if model.flags(index) & QtCore.Qt.ItemIsUserCheckable:
# after the change
new_value = index.data(QtCore.Qt.CheckStateRole)
if last_value != new_value:
self.checked.emit(index, new_value)
return value

class MegaMergeWindow(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
[...]

# controls
self.ui_files = QtGui.QListView()
self.ui_files.setModel(QtGui.QStandardItemModel())
self.ui_files.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
delegate = StyledItemDelegate()
delegate.checked.connect(self.on_checked)
self.ui_files.setItemDelegate(delegate)
[...]

def on_checked(self, index, state):
text = "Checked" if state == QtCore.Qt.Checked else "UnChecked"
item = self.ui_files.model().itemFromIndex(index)
print(item, item.data())
print(index, index.data())
print(state)
print(text)

关于python - 当 QListview 项的复选框更改时发出信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113983/

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