pythonsqlpysideqsqltablemodel

How can i make a QSqlTableModel read-only?


I'm making a programm using PySide6, that will add notes from MainWindow to Journal (there is a QSqlTableModel in it), everything works fine but I cant make the table in Journal non-editable (until you click the "read-only" check-box). I've read a lot of stuff but still cant manage to understand it. Thank you for help

class RolphSecurityService(QMainWindow):
    def __init__(self):
        super(RolphSecurityService, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.conn = Database()
        self.ui.open_journal_btn.clicked.connect(self.open_journal)
        self.ui.do_note.clicked.connect(self.collect_data)
        
    
    def open_journal(self):
        self.jour = Journal()
        self.jour.show()


class Journal(QtWidgets.QDialog):
    def __init__(self):
        super(Journal, self).__init__()
        self.ui = Ui_Journal()
        self.ui.setupUi(self)
        Database()
        self.db = Database()
        self.view_data()
        self.ui.update_list_btn.clicked.connect(self.view_data)
        self.ui.delete_btn.clicked.connect(self.delete_note)
        self.read_only()
        

    def read_only(self):
        ... # I want the func to be here
        
    def view_data(self):

        header_data = [
            '№', 'Имя менеджера', 'Дата запроса', 'Дата обработки',
            'Марка машины', 'Модель машины', 'Цвет машины', 'VIN',
            'СТС', 'Год выпуска', 'Номер ГН', 'Регион ГН',
            'Имя собственника', 'Дата рождения собственника', 'Паспорт собственника', 'ИНН собственника',
            'Владеет с', 'По', 'Номер предоставленный собственника', 'Номер установленный собственника',
            'Чем вызвана сложность'
            ]
        
        header_ranges = range(21)

        self.model = QSqlTableModel(self)
        self.model.setTable('Journal')
        self.model.select()


        for x, y in zip(header_data, header_ranges):
            self.model.setHeaderData(y, QtCore.Qt.Orientation.Horizontal, x)

        self.ui.tableView.setModel(self.model)
        self.ui.tableView.resizeColumnsToContents()
        self.ui.tableView.sortByColumn(0, QtCore.Qt.SortOrder.DescendingOrder)

        x = self.db.get_amount_note()
        self.ui.amount_of_notes.setText(str(x))

    

    def delete_note(self):

        selected = self.ui.tableView.selectedIndexes()

        rows = set(index.row() for index in selected)
        rows = list(rows)
        rows.sort()
        first = rows[0]

        self.model.removeRow(first)
        self.model.select()

I tried using SetEditStrategy and ItemEnabled but that didn't work as planned


Solution

  • You can use EditTriggers on the view.
    To disable editing:

    self.ui.tableView.setEditTriggers(QTableView.EditTriggers.NoEditTriggers)
    

    To enable:

    self.ui.tableView.setEditTriggers(QTableView.EditTriggers.DoubleClicked | QTableView.EditTriggers.SelectedClicked)
    

    Or a different combination of triggers.

    Alternatively, you can subclass QSqlTableModel and reimplement flags() You can make the model read only by not including ItemIsEditable in the ItemFlags it returns.