\n
import sys\nfrom PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QTableView, QHeaderView, QVBoxLayout, QAbstractItemView\nfrom PyQt5.QtCore import Qt, QSortFilterProxyModel\nfrom PyQt5.QtGui import QStandardItemModel, QStandardItem\n\nclass myApp(QWidget):\n def __init__(self):\n super().__init__()\n\n self.resize(1000, 500)\n mainLayout = QVBoxLayout()\n \n tagIDs = ('Tag_1', 'Tag_2', 'Tag_3', 'Tag_4', 'Tag_5')\n descriptions = ('Description_1', 'Description_2', 'Description_3', 'Description_4', 'Description_5')\n\n\n model = QStandardItemModel(len(tagIDs), 2)\n model.itemChanged.connect(self.itemChanged)\n model.setHorizontalHeaderLabels(['Tag IDs', 'Description'])\n\n for i in range(len(tagIDs)):\n \n item1 = QStandardItem(tagIDs[i])\n item1.setCheckable(True)\n \n item2 = QStandardItem(descriptions[i])\n \n model.setItem(i, 0, item1)\n model.setItem(i, 1, item2)\n\n filterProxyModel = QSortFilterProxyModel()\n filterProxyModel.setSourceModel(model)\n filterProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)\n filterProxyModel.setFilterKeyColumn(1)\n \n\n searchField = QLineEdit()\n searchField.setStyleSheet('font-size: 20px; height: 30px')\n searchField.textChanged.connect(filterProxyModel.setFilterRegExp)\n \n mainLayout.addWidget(searchField)\n\n table = QTableView()\n table.setStyleSheet('font-size: 20px;')\n table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)\n table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)\n\n table.setModel(filterProxyModel)\n table.setEditTriggers(QAbstractItemView.NoEditTriggers)\n \n mainLayout.addWidget(table)\n\n\n\n self.setLayout(mainLayout)\n\n def itemChanged(self, item):\n print("Item {!r} checkState: {}".format(item.text(), item.checkState()))\n\n\ndef main():\n \n app = QApplication(sys.argv)\n myAppControl = myApp()\n myAppControl.show()\n\n sys.exit(app.exec_())\n\nif __name__ == "__main__":\n main()\n
\n","author":{"@type":"Person","name":"Juicy"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Header settings that depend on the model must always be set when a model is set.
\nMove table.setModel(filterProxyModel)
right after the creation of the table or, at least, before table.horizontalHeader().setSectionResizeMode
(the vertical setSectionResizeMode()
is generic for the whole header and doesn't cause problems).
Reputation: 35
I am trying to code an application that will allow the user to view a list of Tag IDs, as well as its description, and allow the user to check off each Tag ID that they would like to import data from. At this point I am working on developing the UI only.
The code below worked and would show the application window until I added the itemChanged function & connection. Now, when I run this code, only the print statement from the new function will show. The window never shows and the entire application promptly exits (see image for outcome of running script).
Additionally, you'll notice that we get the checkState of each type of item - I only want the checkState of the Tag ID.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QTableView, QHeaderView, QVBoxLayout, QAbstractItemView
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class myApp(QWidget):
def __init__(self):
super().__init__()
self.resize(1000, 500)
mainLayout = QVBoxLayout()
tagIDs = ('Tag_1', 'Tag_2', 'Tag_3', 'Tag_4', 'Tag_5')
descriptions = ('Description_1', 'Description_2', 'Description_3', 'Description_4', 'Description_5')
model = QStandardItemModel(len(tagIDs), 2)
model.itemChanged.connect(self.itemChanged)
model.setHorizontalHeaderLabels(['Tag IDs', 'Description'])
for i in range(len(tagIDs)):
item1 = QStandardItem(tagIDs[i])
item1.setCheckable(True)
item2 = QStandardItem(descriptions[i])
model.setItem(i, 0, item1)
model.setItem(i, 1, item2)
filterProxyModel = QSortFilterProxyModel()
filterProxyModel.setSourceModel(model)
filterProxyModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
filterProxyModel.setFilterKeyColumn(1)
searchField = QLineEdit()
searchField.setStyleSheet('font-size: 20px; height: 30px')
searchField.textChanged.connect(filterProxyModel.setFilterRegExp)
mainLayout.addWidget(searchField)
table = QTableView()
table.setStyleSheet('font-size: 20px;')
table.verticalHeader().setSectionResizeMode(QHeaderView.Stretch)
table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
table.setModel(filterProxyModel)
table.setEditTriggers(QAbstractItemView.NoEditTriggers)
mainLayout.addWidget(table)
self.setLayout(mainLayout)
def itemChanged(self, item):
print("Item {!r} checkState: {}".format(item.text(), item.checkState()))
def main():
app = QApplication(sys.argv)
myAppControl = myApp()
myAppControl.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Upvotes: 0
Views: 434
Reputation: 48499
Header settings that depend on the model must always be set when a model is set.
Move table.setModel(filterProxyModel)
right after the creation of the table or, at least, before table.horizontalHeader().setSectionResizeMode
(the vertical setSectionResizeMode()
is generic for the whole header and doesn't cause problems).
Upvotes: 1