I am trying to create a window application in Python in the Qt Creator environment. Logic: a window opens (Ui_Ved) it contains a week and an object, a second window (Ui_Graf) opens with the push Button_Work button, data is taken from the table and the button_click button is inserted into the table of the first window. There was a problem with displaying the model in the tableView in the table function.
class Vedomost(QDialog):
def init(self):
super(Vedomost, self).init()
self.first_insert = True
self.ui = Ui_Ved()
self.ui.setupUi(self)
self.ui.pushButton_Work.clicked.connect(self.Work)
self.ui.pushButton_Calendar.clicked.connect(self.show_calendar)
print(self.ui.tableView)
query = QSqlQuery()
self.model = QSqlQueryModel()
def show_calendar(self):
dialog = CalendarDialog(self)
if dialog.exec_():
self.ui.dateEdit.setDate(dialog.selected_date)
def Work(self):
Week = self.ui.dateEdit.date().toString("dd.MM.yyyy")
Obj = self.ui.lineEditObject.text()
self.w = Grafic(Week, Obj)
query = QSqlQuery()
query.prepare("SELECT * FROM work_schedule JOIN object ON work_schedule.object = object.object_id WHERE object.object_name = :object AND start_week = :week")
query.bindValue(":object", Obj)
query.bindValue(":week", Week)
if not query.exec():
print("Ошибка выполнения запроса Work:", query.lastError().text())
self.error()
return
self.w.show()
def table(self, M, Obj, Week):
query = QSqlQuery()
if self.first_insert:
if not query.exec("""
INSERT INTO week_machine(week_machine_id)
VALUES (default);
"""):
print("Ошибка выполнения запроса вставки:", query.lastError().text())
self.first_insert = False
query.prepare("""UPDATE week_machine SET work = :M, start_week = :Week, object = (SELECT object_id FROM object WHERE object.object_name = :Obj)""")
query.bindValue(":M", M)
query.bindValue(":Obj", Obj)
query.bindValue(":Week", Week)
print("в work вставилось", M)
if not query.exec():
print("Ошибка выполнения запроса обновления:", query.lastError().text())
return
query.prepare("""
SELECT m.week_machine_id as "№", w.work_name as "Работа",
m.machine_type as "Тип машины", m.machine_name as "Машина",
m.number as "Кол-во", m.renter as "Арендодатель"
FROM week_machine m
JOIN work w ON m.work = w.work_id
""") # запрос проверен в PSQL
query.exec()
self.model.setQuery(query)
print(self.model.rowCount()) # отвечает 13
self.ui.tableView.setModel(self.model) # проблема!
self.ui.tableView.resizeColumnsToContents()
class Grafic(QDialog):
def init(self, Week, Obj):
super(Grafic, self).init()
self.ui = Ui_Graf()
self.ui.setupUi(self)
self.vvod = Vedomost()
self.ui.pushButton.clicked.connect(self.button_click)
self.ui.pushButton_2.clicked.connect(self.button_click_2)
self.ui.labelWeek.setText(Week)
self.ui.labelWeek.adjustSize()
self.ui.labelObject.setText(Obj)
self.ui.labelObject.adjustSize()
query = QSqlQuery()
query.prepare("""SELECT ws.work_sch_id as "№", w.work_name as "Работа",
u.unit_name as "Ед. Изм.", ws.amount as "Объём", ws.monday as
"Понедельник", ws.tuesday as "Вторник", ws.wednesday as "Среда",
ws.thursday as "Четверг", ws.friday as "Пятница"
FROM work_schedule ws
JOIN object o ON ws.object = o.object_id
JOIN work w ON ws.work = w.work_id
JOIN units u ON ws.unit = u.unit_id
WHERE o.object_name = :object AND ws.start_week = :week""")
query.bindValue(":object", Obj)
query.bindValue(":week", Week)
query.exec_()
self.model = QSqlQueryModel()
self.model.setQuery(query)
self.ui.tableView.setModel(self.model)
self.ui.tableView.resizeColumnsToContents()
def button_click(self):
N = self.ui.lineEdit.text()
query = QSqlQuery()
query.prepare("""
SELECT work
FROM work_schedule
WHERE work_sch_id = :N
""")
query.bindValue(":N", N)
query.exec_()
if query.next():
M = query.value(0)
print("Значение поля work для work_sch_id =", N, ":", M)
self.vvod.table(M, self.Obj, self.Week)
self.close()
else:
self.show_error_message()
Recommendations for changing the code?
Леха Романов is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.