Мне необходимо реализовать в моем приложении возможность добавления строки в Qtablewidget. Таблица строится из массива, при при нажатии на кнопку, вызывается qinputdialog, и данные из него добавляются в массив а далее должны отображаться в в таблице. Но не получается. Нужно емитировать какой то сигнал? либо как то перестроить заново таблицу??
Как будет правильнее! Опытные люди - подскажите! Очень надо, уж столько времени потрачено, а прогресса нет
#!/usr/bin/python3 # -*- coding: utf-8 -*- import json import random from threading import Thread from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QLabel, QTableWidget, QPushButton, QTableWidgetItem, \ QDialog, QMessageBox, QInputDialog import sys import requests import time from login_dialog import Ui_Dialog class Ebo(QWidget): def __init__(self): super().__init__() self.login_status = False self.initUI() def initUI(self): self.data = self.load_data() self.user_list = self.take_user_list() self.setGeometry(300, 300, 925, 738) grid = QGridLayout() self.setLayout(grid) self.label = QLabel() self.label.setText('Метка') grid.addWidget(self.label, 0, 0, 1, 1) self.pushButton = QPushButton() self.pushButton.setText('Login') grid.addWidget(self.pushButton, 0, 1, 1, 1) self.label_2 = QLabel() self.label_2.setText("label_2") grid.addWidget(self.label_2, 1, 0, 1, 1) self.label_3 = QLabel() self.label_3.setText("label_3") grid.addWidget(self.label_3, 1, 1, 1, 1) self.create_user_list_table() grid.addWidget(self.tableWidget, 2, 0, 1, 2) self.tableWidget_2 = QTableWidget() self.tableWidget_2.setObjectName("tableWidget_2") self.tableWidget_2.setColumnCount(4) self.tableWidget_2.setRowCount(1) self.tableWidget_2.horizontalHeader().setStretchLastSection(True) grid.addWidget(self.tableWidget_2, 5, 0, 1, 2) self.pushButton_3 = QPushButton() self.pushButton_3.setText("Add account") grid.addWidget(self.pushButton_3, 4, 1, 1, 1) self.pushButton_2 = QPushButton() self.pushButton_2.setText("Dellete account") grid.addWidget(self.pushButton_2, 4, 0, 1, 1) self.pushButton_4 = QPushButton() self.pushButton_4.setText("Make xls for all") grid.addWidget(self.pushButton_4, 6, 0, 1, 1) self.pushButton_5 = QPushButton() self.pushButton_5.setText("Make xls for this") grid.addWidget(self.pushButton_5, 6, 1, 1, 1) self.dialog = self.dialog_setup() self.login_inst = False self.pushButton.clicked.connect(self.test_one) self.pushButton_3.clicked.connect(self.add_acc) self.pushButton.clicked.connect(self.test_one) self.show() def take_user_list(self): try: return self.data['user_list'] except: return [] def add_acc(self): new_acc = self.show_cr_dial() try: self.data['user_list'].append(new_acc) except: user_list = [] user_list.append(new_acc) self.data['user_list'] = user_list self.create_user_list_table() def load_data(self): try: with open('data.json', 'r') as fp: data = json.load(fp) return data except: data = {'last_run':time.time()} with open('data.json', 'w') as fp: json.dump(data, fp) return data def save_data(self, data): with open('data.json', 'w') as fp: json.dump(data, fp) def get_user_list(self): user_list = ['nelliaksenova', 'lina_belova', 'kosmetika_tai_39'] return user_list def create_user_list_table(self): self.tableWidget = QTableWidget() self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(5) self.tableWidget.setColumnWidth(0, 250) self.tableWidget.setColumnWidth(1, 150) self.tableWidget.setColumnWidth(2, 150) self.tableWidget.setColumnWidth(3, 150) users = len(self.user_list) self.tableWidget.setRowCount(users) self.tableWidget.horizontalHeader().setStretchLastSection(True) row = 0 column = 0 for us in self.user_list: item = QTableWidgetItem(us) self.tableWidget.setItem(row, column, item) row +=1 def show_cr_dial(self): text, ok = QInputDialog.getText(self, 'Create account', 'Enter account name:') if ok: return str(text) def closeEvent(self, event): self.save_data(self.data) reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore() def keyPressEvent(self, e): if e.key() == Qt.Key_Q: self.setGeometry(300, 300, 450, 250) def showLoginDialog(self): pass def test_one(self): if self.dialog.exec_(): self.login, self.passwd = self.ui.get_val() self.login_instagram() def finder(self,txt): text = txt finder_text_start = ('<script type="text/javascript">' 'window._sharedData = ') finder_text_start_len = len(finder_text_start)-1 finder_text_end = ';</script>' all_data_start = text.find(finder_text_start) all_data_end = text.find(finder_text_end, all_data_start + 1) json_str = text[(all_data_start + finder_text_start_len + 1) : all_data_end] all_data = json.loads(json_str) return all_data def login(self): pass def dialog_setup(self): dialog = QDialog() self.ui = Ui_Dialog() self.ui.setupUi(dialog) return dialog def start_thread(self): t = Thread(target=self.load_media) t.daemon = True t.start() def load_media(self): pass if __name__ == '__main__': app = QApplication(sys.argv) ex = Ebo_instagram() sys.exit(app.exec_())