如果在树视图中至少检查了一个元素,我想激活命令链接按钮。
我尝试过这种方法,但效果不佳:
def activate_launch_button(self):
model = self.scenarios.model()
checked_indexes = model.match(model.index(0, 0), QtCore.Qt.CheckStateRole,QtCore.Qt.Checked, -1,QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
print checked_indexes
if checked_indexes != []:
self.launch_btn.setEnabled(True)
此方法的问题是仅在关闭窗口后才激活按钮。我不明白为什么。 为了计算这个方法,我把它放在两个方法中:
def show(self):
self.project.load()
if self.project.tranus_project:
self.tranus_folder.setText(self.project.tranus_project.path)
self.activate_launch_button()
self.launch_options_TRANUS()
super(OptionsTRANUSDialog, self).show()
def select_tranus_folder(self):
folder = QtGui.QFileDialog.getExistingDirectory(self, "Select directory")
if folder:
self.tranus_folder.setText(folder)
if not self.project.load_tranus_folder(folder):
self.tranus_folder.setText('')
self.reload_scenarios()
self.activate_launch_button()
我真的希望在检查至少一个treeview元素时激活命令链接按钮,如果没有选中元素则不激活。
感谢。
答案 0 :(得分:1)
最后,我找到了解决所有问题的方法:
def activate_launch_button(self):
model = self.scenarios.model()
model.itemChanged.connect(self.check_configure)
def check_configure(self,item):
model = self.scenarios.model()
index = model.indexFromItem(item)
if index.data(QtCore.Qt.CheckStateRole) != index.data(QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole):
if index.data(QtCore.Qt.CheckStateRole)!= QtCore.Qt.Unchecked :
self.count_check+=1
model.setData(index,index.data(QtCore.Qt.CheckStateRole),QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
else :
self.count_check-=1
model.setData(index,index.data(QtCore.Qt.CheckStateRole),QtCore.Qt.UserRole + QtCore.Qt.CheckStateRole)
print self.count_check
self.launch_btn.setEnabled(self.count_check>0)
答案 1 :(得分:0)
最后,我找到了问题的部分解决方案,我使用了信号itemChanged http://pyqt.sourceforge.net/Docs/PyQt4/qstandarditemmodel.html#itemChanged
def activate_launch_button(self):
model = self.scenarios.model()
model.itemChanged.connect(self.check_configure)
def check_configure(self):
self.launch_btn.setEnabled(True)
当检查到一个元素时激活按钮但是当我取消选中所有元素时,该按钮不会变为非活动状态。我该如何解决这个问题?