New to python so sorry for the terrible code but I'm trying to change something in my code when the user selects a checkbox from the combo box but the program crashes when I try. I can load the table fine but when I try to get the data in my LoadCombo(self): method the program crashes. Thanks for any advice
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog, QTableView, QTableWidget, qApp
from PyQt5.QtCore import Qt, QModelIndex, QAbstractTableModel, QSortFilterProxyModel
import sys, os
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(90, 50, 691, 491))
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.verticalLayoutWidget = QtWidgets.QWidget(self.tab)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(70, 50, 491, 331))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.testcombo = CheckableComboBox(self.verticalLayoutWidget)
self.testcombo.setObjectName("testcombo")
self.horizontalLayout.addWidget(self.testcombo)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.testcombo1 = CheckableComboBox(self.verticalLayoutWidget)
self.testcombo1.setObjectName("testcombo1")
for i in range(3):
self.testcombo1.addItem("Combobox Item " + str(i))
self.horizontalLayout.addWidget(self.testcombo)
self.horizontalLayout.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.LoadTable)
#self.testcombo.itemChecked.connect(self.LoadTable)
self.verticalLayout.addWidget(self.pushButton)
self.table = QtWidgets.QTableView()
self.LoadTable()
self.proxyModel = QSortFilterProxyModel()
self.proxyModel.setSourceModel(self.model)
self.table.setSortingEnabled(True)
self.table.setModel(self.proxyModel)
self.verticalLayout.addWidget(self.table)
self.LoadCombo()
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.tabWidget.addTab(self.tab_2, "")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.LoadComboBoxes()
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Load Table"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
def LoadTable(self):
#print(self.testcombo.currentData())
#self.headerData(['Column 1test', 'Column 2test', 'Column 3', 'test'], Qt.Horizontal, Qt.DisplayRole)
data = [self.testcombo.currentData()]
#print(data)
headers = []
for x in self.testcombo.currentData():
headers.append(x+'TESTING!')
print(headers)
#headers = self.testcombo.currentData()
self.model = TableModel(data, headers)
self.proxyModel = QSortFilterProxyModel()
self.proxyModel.setSourceModel(self.model)
#self.table.setSortingEnabled(True)
self.table.setModel(self.proxyModel)
#self.setCentralWidget(self.table)
def LoadCombo(self):
#As soon as I uncomment the below the program crashes when I select a checkbox
#print(self.testcombo.currentData())
print('test')
def LoadComboBoxes(self):
for i in range(6):
self.testcombo.addItem("Combobox Item " + str(i))
class CheckableComboBox(QtWidgets.QComboBox, Ui_MainWindow):
def __init__(self, parent=None):
super(CheckableComboBox, self).__init__(parent)
self.view().pressed.connect(self.handleItemPressed)
# once there is a checkState set, it is rendered
# here we assume default Unchecked
def addItem(self, item):
super(CheckableComboBox, self).addItem(item)
item = self.model().item(self.count()-1,0)
#item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
item.setFlags(QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Unchecked)
def handleItemPressed(self, index):
self.LoadCombo()
item = self.model().itemFromIndex(index)
# checking if item is checked
if item.checkState() == Qt.Checked:
# making it unchecked
item.setCheckState(Qt.Unchecked)
# if not checked
else:
# making the item checked
item.setCheckState(Qt.Checked)
def itemChecked(self, index):
item = self.model().item(i,0)
return item.checkState() == QtCore.Qt.Checked
def currentData(self):
# Return the list of selected items data
res = []
for i in range(self.model().rowCount()):
if self.model().item(i).checkState() == Qt.Checked:
res.append(self.model().item(i).text())
return res
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data, headers):
super(TableModel, self).__init__()
self._data = data
self.header_labels = headers
def data(self, index, role):
if role == Qt.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return self._data[index.row()][index.column()]
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.header_labels[section]
return QAbstractTableModel.headerData(self, section, orientation, role)
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length)
return len(self._data[0])
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Aucun commentaire:
Enregistrer un commentaire