我正在将QListWidget中的小部件设置为我自己的ProductItemWidget类,以便能够对列表中的项进行更多的定制。这很好,但我无法让列表自动排序。我打电话来
my_list.setSortingEnabled(True)
然后我尝试覆盖ProductListItem的“<;”比较器,我创建它是为了能够覆盖这个函数。在widget中,比较了getti()和它们调用的两个函数。这里的问题是,默认情况下,函数将第二个参数强制转换为QListWidgetItem,因此在我的代码中,我为self获取ProductListItem,而为otherItem获取QListWidgetItem。otherItem不允许我访问它对应的ProductItemWidget,因为访问它的唯一方法是将ProductListItem传递给QListWidget的itemWidget()调用。这是我的代码:
^{pr2}$有什么方法可以防止调用\ult\uuuu将otherItem强制转换为QListWidgetItem吗?在
我已经在这个问题上纠结了一段时间,所以任何建议都将不胜感激。我愿意改变我的整个方法。在
QListWidget是“便利”类之一(如QTreeWidget和QTableWidget)。只要您的需求非常简单,使用它们就可以了。但一旦你想要一个更复杂的东西,这种僵化很快就会显现出来。在
通过切换到具有QStandardItemModel的更通用的QListView类,您可以相当容易地解决问题。这需要更多的工作来设置,但它将立即带来更多的灵活性。在
下面是基于示例代码的该方法的演示:
相关问题 更多 >
编程相关推荐