【问题标题】:PyQt5 - Getting information from a QcomboBox on TableWidgetPyQt5 - 从 TableWidget 上的 QcomboBox 获取信息
【发布时间】:2021-05-31 08:28:57
【问题描述】:

我正在寻找一些代码,使我能够获取组合框的信息,我将它放在 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

提前感谢您的帮助。

【问题讨论】:

    标签: python combobox pyqt pyqt5 qtableview


    【解决方案1】:

    使用单元格小部件绝对不会与表格的模型及其数据进行交互。

    如果您需要访问特定的单元格小部件,请使用与setIndexWidget()indexWidget()相反的getter函数:

    combo = tableview.indexWidget(tableview.model().index(0,4))
    combo_index = combo.currentIndex()
    

    【讨论】:

    • 我不明白这些行应该做什么。我正在尝试使用另一个 def 来访问 tableview 上组合框中的数据,但是当我在另一个 def 中运行这些行时,程序崩溃了。以下是完整代码:pastebin.com/QNH3U4Yj
    • 现在我明白了,当您说您“不使用任何课程”时。不幸的是,非常不建议像这样使用 Qt,它只会使事情变得比它们所能做的复杂得多。尝试了解我的代码的作用并阅读相关文档。然后我强烈建议你研究类的工作原理并尝试获得经验,因为没有这些知识,你几乎不会在任何语言或框架上走得太远,而且使用 Python 或 Qt 等系统不仅会遇到更多困难,但人们不太可能真正帮助您。
    【解决方案2】:

    我找到了解决我的问题的方法,正如 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()
    

    我从 Jie Jenn https://www.youtube.com/watch?v=KMJTJNUzo4E 那里拿了代码

    希望这对将来一些无知的程序员有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多