class AddPass(QtWidgets.QDialog): def __init__(self, id=None): super(AddPass, self).__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.buttonBox.accepted.connect(self.add) self.ui.buttonBox.rejected.connect(self.close) self.bd = Orm() self.id = id if self.id: data = self.bd.get_change(self.id) self.ui.lineEdit_1.setText(str(data[0][1])) self.ui.lineEdit_2.setText(str(data[0][2])) self.ui.lineEdit_3.setText(str(data[0][3])) def add(self): name = self.ui.lineEdit_1.text() login = self.ui.lineEdit_2.text() password = self.ui.lineEdit_3.text() if self.id: self.bd.del_log(self.id) self.bd.changelog(self.id, name, login, password) else: self.bd.add_log(name, login, password) self.close()
class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.tableWidget.setSelectionBehavior( QTableView.SelectionBehavior.SelectRows) self.ui.pushButton_1.clicked.connect(self.add_log) self.ui.pushButton_2.clicked.connect(self.del_log) self.ui.pushButton_3.clicked.connect(self.search) self.ui.pushButton_4.clicked.connect(self.to_main) self.ui.pushButton_5.clicked.connect(self.change_data) self.ui.pushButton_4.hide() self.id = False self.bd = Orm() self.now(self.bd.all_log()) def now(self, data): if data: self.ui.tableWidget.setEnabled(True) self.ui.pushButton_3.setEnabled(True) self.ui.pushButton_4.setEnabled(True) # ряды и столбцы self.ui.tableWidget.setRowCount(len(data)) self.ui.tableWidget.setColumnCount(len(data[0])) self.ui.tableWidget.setHorizontalHeaderLabels(( 'Id', 'Название приложения', 'Логин', 'Пароль', )) row = 0 for tup in data: col = 0 for item in tup: cellinfo = QTableWidgetItem(str(item)) cellinfo.setFlags(QtCore.Qt.ItemFlags.ItemIsSelectable | QtCore.Qt.ItemFlags.ItemIsEnabled) self.ui.tableWidget.setItem(row, col, cellinfo) col += 1 row += 1 self.ui.tableWidget.resizeColumnsToContents() # self.ui.tableWidget.horizontalHeader().setSectionResizeMode(col - 1, QHeaderView.stretchSectionCount) self.ui.tableWidget.horizontalHeader().setStretchLastSection( True) else: self.ui.tableWidget.clear() self.ui.tableWidget.setEnabled(False) self.ui.pushButton_3.setEnabled(False) self.ui.pushButton_4.setEnabled(False) def add_log(self): self.dualog = AddPass() self.dualog.exec() self.now(self.bd.all_log()) def change_data(self): if not self.id: self.now(self.bd.all_log()) msg = QMessageBox() msg.setWindowTitle("Ошибка") msg.setText("Вы не выбрали не одну запись") msg.addButton('Ок', QMessageBox.ButtonRole.RejectRole) msg.exec() else: self.dualog = AddPass(self.id) self.dualog.exec() self.now(self.bd.all_log()) self.id = False def del_log(self): if not self.id: self.now(self.bd.all_log()) msg = QMessageBox() msg.setWindowTitle("Ошибка") msg.setText("Вы не выбрали не одну запись") msg.addButton('Ок', QMessageBox.ButtonRole.RejectRole) msg.exec() else: self.bd.del_log(self.id) self.now(self.bd.all_log()) self.id = False @pyqtSlot(QModelIndex) def on_tableWidget_clicked( self, index: QModelIndex): # получение индекса строки при нажатие self.id = int(self.ui.tableWidget.item(index.row(), 0).text()) @pyqtSlot(QModelIndex) def on_tableWidget_doubleClicked( self, index: QModelIndex): # получение списка обьектов r = self.ui.tableWidget.item(index.row(), index.column()).text() clipboard.setText(r) def search(self): self.ui.pushButton_3.hide() self.ui.pushButton_4.show() self.search_dialog = InputDialog(self) self.search_dialog.exec() def to_main(self): self.ui.pushButton_4.hide() self.ui.pushButton_3.show() self.now(self.bd.all_log())