Let closeEvent interract with Model/View
-
Hi all,
I have a working model table based.
The user interacts with the model though a QDialog (child of the main QMainWindow).
I'd like to update a file (with some of the table's data) only when the user closes this subwindow and not when data is changed.The idea is to reimplement the QCloseEvent adding an emitting signal that calls the slot (method) to make all the necessary stuff.
My problem is related to the "architecture"; I can't find a way to make this signal visible to both the dialog and the data, here the skeleton code:_data.py
class TableModel(QAbstractTableModel): ... def data(self, ...): .... def setData(self, ...): .... class Data(QObject): table_changed = pyqtSignal(object) data_updated = pyqtSignal() def __init__(self, ...): ... def update_to_model(self): .... def update_file(self, ...): .... class DialogData(QDialog): def __init__(self, data): ... view = QTableView(self) ... vbox = QVBoxLayout(self) vbox.addWidget(view) ... def closeEvent(self, event): # I can't (of course) emit this signal - out of scope self.data_updated.emit() ... event.accept()_main_window.py
class MainWindow(QMainWindow): def __init(self, data) ... self.dialog_window = DialogData(data) ..._main.py
def main() ... # here where model il created and managed data = Data() # here where main window is created and managed main_window = MainWindow(data) ... # connect signal to the slot data.data_updated.connect(data.update_file) ...I hope that the sketch is clear.
What are the necessary changes to my architecture to let the closeEvent within the dialog_window emit a signal to be used from the slot update_file?
Many thanks to all.
-
Hi all,
I have a working model table based.
The user interacts with the model though a QDialog (child of the main QMainWindow).
I'd like to update a file (with some of the table's data) only when the user closes this subwindow and not when data is changed.The idea is to reimplement the QCloseEvent adding an emitting signal that calls the slot (method) to make all the necessary stuff.
My problem is related to the "architecture"; I can't find a way to make this signal visible to both the dialog and the data, here the skeleton code:_data.py
class TableModel(QAbstractTableModel): ... def data(self, ...): .... def setData(self, ...): .... class Data(QObject): table_changed = pyqtSignal(object) data_updated = pyqtSignal() def __init__(self, ...): ... def update_to_model(self): .... def update_file(self, ...): .... class DialogData(QDialog): def __init__(self, data): ... view = QTableView(self) ... vbox = QVBoxLayout(self) vbox.addWidget(view) ... def closeEvent(self, event): # I can't (of course) emit this signal - out of scope self.data_updated.emit() ... event.accept()_main_window.py
class MainWindow(QMainWindow): def __init(self, data) ... self.dialog_window = DialogData(data) ..._main.py
def main() ... # here where model il created and managed data = Data() # here where main window is created and managed main_window = MainWindow(data) ... # connect signal to the slot data.data_updated.connect(data.update_file) ...I hope that the sketch is clear.
What are the necessary changes to my architecture to let the closeEvent within the dialog_window emit a signal to be used from the slot update_file?
Many thanks to all.
@superaga said in Let closeEvent interract with Model/View:
data.data_updated.connect(data.update_file)
What is the reasoning behind this? Why do you connect a signal and slot from same instance?
Back to your question: override closeEvent, define a signal in MainWindow, emit that signal in closeEvent and connect that signal to the slot:
main_window.YOUR_SIGNAL.connect(data.update_file)