Python Pyqt 在两个表之间同步排序
Python Pyqt Sync Sorting Between Two Tables
我一直在尝试找到一个解决方案或示例来将我的 table1
的列排序与我的 table2
同步,table1
具有与 table2
相关联的值,它们的行数相同,只有 table1
会被排序,但 table2
可能包含一些按钮。我找到了一个示例 here 但不幸的是它在 C++
中,我真的试图理解它但我不能。如何在保持 table2
的行与 table1
同步的同时对 table1
进行排序?我正在使用 QTableWidget
.
____________ ______________
| table1 | | table2 |
|----------| |-------------|
|DDD | |data of DDD |
|----------| |-------------|
|AAAA | |data of AAAA |
|----------| |-------------|
|CCCC | |data of CCCC |
|__________| |_____________|
我使用了两个表,因为我使用 table1
作为 frozen
列。
我可以在 table2
中使用一个隐藏的列,其中该列的值可以用作 id
或引用重新同步 table2
但我想避免这样做一种非常缓慢的排序方式。
一个可能的解决方案是将 QTableViews 与模型和 QSortFilterProxyModel 一起使用,因此当您订购 QTableView 时,您将订购模型,该模型将通知其他 QTableView。
此解决方案可以节省资源,因为它只订购一个元素:模型。
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
model = QtGui.QStandardItemModel()
proxy = QtGui.QSortFilterProxyModel()
proxy.setSourceModel(model)
table1 = QtGui.QTableView()
table1.setModel(proxy)
table1.setSortingEnabled(True)
table2 = QtGui.QTableView()
table2.setModel(proxy)
for i in range(100):
for j, w in enumerate([QtGui.QLineEdit, QtGui.QPushButton, QtGui.QCheckBox]):
text = "{}-{}".format(i,j)
it = QtGui.QStandardItem()
if j == 0:
it.setText(text)
model.setItem(i, j, it)
ix = model.indexFromItem(it)
widget = w(text)
table2.setIndexWidget(proxy.mapFromSource(ix), widget)
for i in range(1, proxy.columnCount()):
table1.setColumnHidden(i, True)
lay = QtGui.QVBoxLayout(self)
splitter = QtGui.QSplitter()
splitter.addWidget(table1)
splitter.addWidget(table2)
lay.addWidget(splitter)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
我一直在尝试找到一个解决方案或示例来将我的 table1
的列排序与我的 table2
同步,table1
具有与 table2
相关联的值,它们的行数相同,只有 table1
会被排序,但 table2
可能包含一些按钮。我找到了一个示例 here 但不幸的是它在 C++
中,我真的试图理解它但我不能。如何在保持 table2
的行与 table1
同步的同时对 table1
进行排序?我正在使用 QTableWidget
.
____________ ______________
| table1 | | table2 |
|----------| |-------------|
|DDD | |data of DDD |
|----------| |-------------|
|AAAA | |data of AAAA |
|----------| |-------------|
|CCCC | |data of CCCC |
|__________| |_____________|
我使用了两个表,因为我使用 table1
作为 frozen
列。
我可以在 table2
中使用一个隐藏的列,其中该列的值可以用作 id
或引用重新同步 table2
但我想避免这样做一种非常缓慢的排序方式。
一个可能的解决方案是将 QTableViews 与模型和 QSortFilterProxyModel 一起使用,因此当您订购 QTableView 时,您将订购模型,该模型将通知其他 QTableView。
此解决方案可以节省资源,因为它只订购一个元素:模型。
from PyQt4 import QtCore, QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
model = QtGui.QStandardItemModel()
proxy = QtGui.QSortFilterProxyModel()
proxy.setSourceModel(model)
table1 = QtGui.QTableView()
table1.setModel(proxy)
table1.setSortingEnabled(True)
table2 = QtGui.QTableView()
table2.setModel(proxy)
for i in range(100):
for j, w in enumerate([QtGui.QLineEdit, QtGui.QPushButton, QtGui.QCheckBox]):
text = "{}-{}".format(i,j)
it = QtGui.QStandardItem()
if j == 0:
it.setText(text)
model.setItem(i, j, it)
ix = model.indexFromItem(it)
widget = w(text)
table2.setIndexWidget(proxy.mapFromSource(ix), widget)
for i in range(1, proxy.columnCount()):
table1.setColumnHidden(i, True)
lay = QtGui.QVBoxLayout(self)
splitter = QtGui.QSplitter()
splitter.addWidget(table1)
splitter.addWidget(table2)
lay.addWidget(splitter)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())