Good all the time of day. I started to learn PyQt, but as long as I’m advancing tightly, I don’t understand the ideology, so there are problems from scratch.
There is an unpretentious database from one tablet - it doesn't matter. My code implements a window where there is a QTableView table and 2 buttons. When I click on the button, I need to get the cell text of the currently selected row, BUT the first column.
I was able to get the index of the selected cell (row, column), I was also able to get the value of the current cell, but I just can’t get exactly the cell by row and column, how can I do it?
Here is the code: clicking on the button - def addTotal (self):
import sys from PyQt5 import QtCore,QtGui,QtWidgets,QtSql from framework import options class EditRating(QtWidgets.QWidget): stm = None tv = None def __init__(self, parent=None): QtWidgets.QWidget.__init__(self, parent) # инициализация # описание всех объектов в окне self.ModelsTable = QtWidgets.QTableView() self.ModelsTable.setSelectionMode(1) # может быть выделен только один элемент self.addSuccesButton = QtWidgets.QPushButton("Добвить рейтинг модели (РЕЙТИНГ)") self.addTotalButton = QtWidgets.QPushButton("Увеличить общий счетчик модели (СЧЕТЧИК)") # задать параметры объектов окна self.addTotalButton.setMinimumHeight(50) self.addSuccesButton.setMinimumHeight(50) # создать вертикальный box self.vbox = QtWidgets.QVBoxLayout() # добавить в вертикальный box виджеты self.vbox.addWidget(self.ModelsTable) self.vbox.addWidget(self.addSuccesButton) self.vbox.addWidget(self.addTotalButton) self.setLayout(self.vbox) # обработчики событий нажатия на кнопки self.addSuccesButton.clicked.connect(self.addSucces) self.addTotalButton.clicked.connect(self.addTotal) # увеличение рейтинга def addSucces(self): # получить имя выбранной модели pass # row = self.ModelsTable.currentIndex().row() col = self.ModelsTable.currentIndex().column() # ЗДЕСЬ МНЕ НУЖНО ПОЛУЧИТЬ ТЕКСТ ЯЧЕЙКИ ТЕКУЩЕЙ СТРОКИ И ПЕРВОГО СТОЛБЦА print(row,col) # увеличение счетчика def addTotal(self): print("total") # загрузка списка моделей в таблицу def loadModelsList(self): # подключить базу данных path_to_priceDB = options.GetOptions("SQLite") con = QtSql.QSqlDatabase.addDatabase('QSQLITE') con.setDatabaseName(path_to_priceDB) con.open() # создать модель stm = QtSql.QSqlQueryModel(parent=window) stm.setQuery('select * FROM MODELS') # задать заголовки для столбцов модели stm.setHeaderData(1,QtCore.Qt.Horizontal,"Название модели") stm.setHeaderData(2, QtCore.Qt.Horizontal, "Описание модели") tv = self.ModelsTable # задаем для таблицы созданную модель tv.setModel(stm) # скрыть ненужные столбцы tv.hideColumn(0) # скрыть ID tv.hideColumn(3) # скрыть source tv.hideColumn(4) # скрыть символ tv.hideColumn(5) # скрыть таймфрейм tv.hideColumn(7) # скрыть тип модели # установить размеры колонок tv.setColumnWidth(1,180) # название модели tv.setColumnWidth(2, 400) # описание модели # передать данные в класс self.stm = stm self.tv = tv if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = EditRating() window.setWindowTitle("Программа редактирования рейтинга моделей") window.resize(1024,600) window.loadModelsList() # загрузка списка моделей window.show() sys.exit(app.exec_())