如何解决PyQt5 - 从 TableWidget 上的 QcomboBox 获取信息
我正在寻找一段代码,使我能够获取组合框的信息,该组合框已放在由 QtDesigner 创建的 TableView 上。
由于缺乏知识,我没有使用任何类或委托。当我使用 TableWidget 时,行 TableWidget.cellWidget(indexofthecurrentrow,4).currentIndex() == 0
向我返回了行的当前状态,允许我更新数据库,但由于模型或没有委托,该行在我假设的 TableView 上不起作用。
相关代码如下:
for row in range(len(data_from_sqlite)):
comboBox = QComboBox()
comboBox.addItems('opt1','opt2')
index_combo = tableview.model().index(row,5)
tableview.setIndexWidget(index_combo,comboBox)
我只是不知道如何通过按钮中连接的其他功能检索此 QComboBox 状态。 我都试过了
tableview.model().index(0,4).itemData().currentIndex() #crashes
和
tableview.model().index(0,4).data() #returns None
在此先感谢您的帮助。
解决方法
使用单元格小部件绝对没有与表格模型及其数据交互。
如果您需要访问特定的单元格小部件,请使用与 setIndexWidget()
、indexWidget()
相对的 getter 函数:
combo = tableview.indexWidget(tableview.model().index(0,4))
combo_index = combo.currentIndex()
,
我找到了解决我的问题的方法,正如 musicamante 所指出的,该解决方案涉及使用女巫类非常基础,对我来说非常困难。
tableview中实现combobox的循环是:
for row in range(len(data)):
combo = combomaker(self)
index_combo = self.tableview.model().index(row,5)
self.tableview.setIndexWidget(index_combo,combo)
成为组合制造商,如下
class combo(QComboBox):
def __init__(self,parent):
super().__init__(parent)
self.addItems(['a','b'])
self.currentIndexChanged.connect(self.getComboValue)
def getComboValue(self):
print(self.currentText())
# return self.currentText()
我从杰珍那里拿了代码https://www.youtube.com/watch?v=KMJTJNUzo4E
希望这对未来一些无知的程序员有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。