from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1069, 791)
Form.setWindowTitle("")
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.category = QtWidgets.QTableWidget(Form)
self.category.setObjectName("category")
self.category.setColumnCount(3)
self.category.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.category.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.category.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.category.setHorizontalHeaderItem(2, item)
self.horizontalLayout_2.addWidget(self.category)
self.subcategory = QtWidgets.QTableWidget(Form)
self.subcategory.setObjectName("subcategory")
self.subcategory.setColumnCount(3)
self.subcategory.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.subcategory.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.subcategory.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.subcategory.setHorizontalHeaderItem(2, item)
self.horizontalLayout_2.addWidget(self.subcategory)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
item = self.category.horizontalHeaderItem(0)
item.setText(_translate("Form", "Категория"))
item = self.category.horizontalHeaderItem(1)
item.setText(_translate("Form", "К-во"))
item = self.category.horizontalHeaderItem(2)
item.setText(_translate("Form", "Цена"))
item = self.subcategory.horizontalHeaderItem(0)
item.setText(_translate("Form", "Модель"))
item = self.subcategory.horizontalHeaderItem(1)
item.setText(_translate("Form", "К-во"))
item = self.subcategory.horizontalHeaderItem(2)
item.setText(_translate("Form", "Штрихкод"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QTableWidgetItem
import test_sort as my_gui
category_data = (("Категория 1", "100", "300.00"),
("Категория 2", "1000", "200.00"),
("Категория 3", "20", "120.00")
)
subcategory_data = {"Категория 1":
{
("Ч1", "5", "100000000001"),
("Ч2", "6", "100000000002"),
("Ч3", "7", "100000000003"),
("Ч4", "8", "100000000004"),
("Ч5", "9", "100000000005"),
("Ч6", "10", "100000000006"),
("Ч7", "100", "100000000007")
},
"Категория 2":
{
("C1", "5", "200000000001"),
("C2", "4", "200000000002"),
("C3", "3", "200000000003"),
("C4", "2", "200000000004"),
("C5", "1", "200000000005")
},
"Категория 3": {
("Ф1", "50", "300000000001"),
("Ф2", "40", "300000000002"),
("Ф3", "30", "300000000003"),
("Ф4", "20", "300000000004"),
("Ф5", "10", "300000000005")
}
}
class Window(QtWidgets.QWidget, my_gui.Ui_Form):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setupUi(self)
self.fill_table_category()
self.category.itemSelectionChanged.connect(self.fill_table_subcategory)
self.category.selectRow(0)
def fill_table_category(self):
self.category.setRowCount(0)
for row in enumerate(category_data, 0):
self.category.insertRow(row[0])
for column in range(self.category.columnCount()):
self.category.setItem(row[0], column, QTableWidgetItem(str(row[1][column])))
self.category.setSortingEnabled(True)
def fill_table_subcategory(self):
self.subcategory.setRowCount(0)
data_for_subcategory = self.get_selected_category()
for row in enumerate(subcategory_data[data_for_subcategory]):
self.subcategory.insertRow(row[0])
for column in range(self.subcategory.columnCount()):
self.subcategory.setItem(row[0], column, QTableWidgetItem(str(row[1][column])))
self.subcategory.setSortingEnabled(True)
def get_selected_category(self):
row_index = self.category.currentRow()
selected_category = self.category.item(row_index, 0).text()
return selected_category
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
def fill_table_subcategory(self):
self.subcategory.setRowCount(0)
data_for_subcategory = self.get_selected_category()
self.subcategory.setSortingEnabled(False) # <- вот этой строкой
for row in enumerate(subcategory_data[data_for_subcategory]):
self.subcategory.insertRow(row[0])
for column in range(self.subcategory.columnCount()):
self.subcategory.setItem(row[0], column, QTableWidgetItem(str(row[1][column])))
self.subcategory.setSortingEnabled(True)