'How to keep the widget when use QSortFilterProxyModel?
I'm making a bulletin board using a table view now. Here, the proxy model is used for the search function, and all contents are well output. However, I want to insert listwidget for the attached file part, but it doesn't work well. First, the width disappears after searching. Second, I want to set the size of the listwidget to the size inherited from the internal row.
Normal [![enter image description here][1]][1]
Search and Reset(delete search keyword)
[![enter image description here][2]][2]
↑ If you search and reset it, the widget is gone.
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Buttons(QWidget):
def __init__(self):
super().__init__()
layout = QHBoxLayout(self)
layout.addWidget(QPushButton('btn1'))
layout.addWidget(QPushButton('btn2'))
self.setLayout(layout)
class MainWindow(QWidget):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setGeometry(200,200,1000,1000)
self.tab = QTableView()
self.sti = QStandardItemModel()
self.sti.setHorizontalHeaderLabels(["Date","Name","Content","Attach"])
proxy = QSortFilterProxyModel()
proxy.setSourceModel(self.sti)
proxy.setFilterKeyColumn(-1)
searchbar=QLineEdit()
searchbar.textChanged.connect(proxy.setFilterRegExp)
self.tab.setModel(proxy)
self.tab.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tab.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.get_list()
layout = QVBoxLayout()
layout.addWidget(searchbar)
layout.addWidget(self.tab)
self.setLayout(layout)
def get_list(self):
data = {
'Date': [2016, 2017, 2018],
'Name': [2.8, 3.1, 3.0],
'Content': ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', '1.73M', '1.83M'],
'Attach':[[1,2],[3,4],[5,6]]
}
df = pd.DataFrame(data)
for i in range(len(df.index)):
for j in range(len(df.columns)):
item = QStandardItem(str(df.iloc[i,j]))
item.setFlags(item.flags() ^ Qt.ItemIsEditable)
self.sti.setItem(i,j,item)
# attachment list
if j == 3:
list = QListWidget()
list.insertItem(0,"www.google.com/attach1")
list.insertItem(1, "www.google.com/attach1")
list.insertItem(2, "www.google.com/attach1")
list.insertItem(3, "www.google.com/attach1")
self.tab.setIndexWidget(self.tab.model().index(i,j),list)
app = QApplication([])
window = MainWindow()
window.resize(800, 600)
window.show()
app.exec_() ```
[1]: https://i.stack.imgur.com/xSdaR.png
[2]: https://i.stack.imgur.com/XZxoY.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
