0

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

GonnaBeDev
  • 13
  • 3
  • What do you mean by "until you click the 'read only' check box"? Please try to explain more clearly what happens and what you actually expected to do, also include the code of your attempts, even if they didn't work. – musicamante Jul 31 '23 at 09:56
  • I've a check box in the journal window. It's checked by default, and you cant edit anything until you uncheck it. Cant give you the code i tried, sorry. Thanks for helping – GonnaBeDev Jul 31 '23 at 10:08

1 Answers1

0

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.

mahkitah
  • 562
  • 1
  • 6
  • 19
  • How can i use the alternative variant? It seems more suitable, thanks – GonnaBeDev Jul 31 '23 at 10:04
  • I've tried your variant, it works just fine. Is there any way i can get value of EditTriggers, so I can disable the delete function while table is read-only – GonnaBeDev Jul 31 '23 at 10:14
  • @GonnaBeDev just like almost all Qt functions, each `setSomeValue()` usually has a `someValue()` function. Alternatively, [read the documentation](https://doc.qt.io/qt-5/qabstractitemview.html#editTriggers-prop). Besides, since this is done by code, you should make easier to know if the user will be able to delete or not: you shouldn't disable the function, you should disable the button. – musicamante Jul 31 '23 at 10:24
  • @musicamante That makes sense, thank you – GonnaBeDev Jul 31 '23 at 11:04