Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Let closeEvent interract with Model/View

Let closeEvent interract with Model/View

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 606 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • superagaS Offline
    superagaS Offline
    superaga
    wrote on last edited by superaga
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • superagaS superaga

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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)
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • superagaS Offline
        superagaS Offline
        superaga
        wrote on last edited by
        #3

        @jsulm many thanks for your answer.

        Sorry that is a typo (let me correct it...)

        Your answer makes sense and clarify the point.

        Many thanks!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved