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. Copying one table widget onto two tabs
Qt 6.11 is out! See what's new in the release blog

Copying one table widget onto two tabs

Scheduled Pinned Locked Moved Unsolved Qt for Python
11 Posts 5 Posters 2.0k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    Ed Schneider
    wrote on last edited by
    #1

    I need to use the same QTable Widget on two tabs. The only way I've found to do this is to create the widget in a class and to create two instances of that class. When the Table is complex that seems inefficient. Is there a better way? Deepcopy doesn't do it as far as I can tell. I'm using PySide6.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • E Ed Schneider

      I need to use the same QTable Widget on two tabs. The only way I've found to do this is to create the widget in a class and to create two instances of that class. When the Table is complex that seems inefficient. Is there a better way? Deepcopy doesn't do it as far as I can tell. I'm using PySide6.

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

      @Ed-Schneider You should better use QTableView with a model.

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

      1 Reply Last reply
      3
      • E Ed Schneider

        I need to use the same QTable Widget on two tabs. The only way I've found to do this is to create the widget in a class and to create two instances of that class. When the Table is complex that seems inefficient. Is there a better way? Deepcopy doesn't do it as far as I can tell. I'm using PySide6.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Ed-Schneider
        In Qt QWidgets cannot be copied, and that extends up to your QTableWidget.

        Follow @jsulm's advice (separate QTableViews per tab, shared model).

        E 1 Reply Last reply
        1
        • JonBJ JonB

          @Ed-Schneider
          In Qt QWidgets cannot be copied, and that extends up to your QTableWidget.

          Follow @jsulm's advice (separate QTableViews per tab, shared model).

          E Offline
          E Offline
          Ed Schneider
          wrote on last edited by
          #4

          @JonB I've been reading up on QTableView. Unfortunately what I've read doesn't address the need for the copy in each tab. I'm trying to avoid the rebuilding of the Widget and it's unclear that QTableView will avoid that. Here's the code where I now create an instance (per tab) of the class that builds the Widget and add it to the layout of each tab. Is this where I would add it to each tab, note I do it differently in each tab, one replaces the current content of the tab, the other adds it to the bottom of the tab with a spacer. Is this where I would use the QTableView or would that go in the class where I build the widget - BuildOutputForGUI ?

          	def output_to_gui(self):
          
          		for i in reversed(range(self.tab_output_layout.count())):
          			self.tab_output_layout.itemAt(i).widget().setParent(None)
          		#
          		table_to_output = BuildOutputForGUI(self, self.active, self.target)
          		#
          		# Add Command Specific table to Output tab
          		#
          		self.tab_output_layout.addWidget(table_to_output)
          		#
          		table_to_log = BuildOutputForGUI(self, self.active, self.target)
          		#
          		# Add spacer and Command Specific table to Log tab
          		#
          		spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Fixed)  # Adjust size as needed
          		self.tab_log_layout.addItem(spacer)
          		#
          		self.tab_log_layout.addWidget(table_to_log)
          
          JonBJ 1 Reply Last reply
          0
          • E Ed Schneider

            @JonB I've been reading up on QTableView. Unfortunately what I've read doesn't address the need for the copy in each tab. I'm trying to avoid the rebuilding of the Widget and it's unclear that QTableView will avoid that. Here's the code where I now create an instance (per tab) of the class that builds the Widget and add it to the layout of each tab. Is this where I would add it to each tab, note I do it differently in each tab, one replaces the current content of the tab, the other adds it to the bottom of the tab with a spacer. Is this where I would use the QTableView or would that go in the class where I build the widget - BuildOutputForGUI ?

            	def output_to_gui(self):
            
            		for i in reversed(range(self.tab_output_layout.count())):
            			self.tab_output_layout.itemAt(i).widget().setParent(None)
            		#
            		table_to_output = BuildOutputForGUI(self, self.active, self.target)
            		#
            		# Add Command Specific table to Output tab
            		#
            		self.tab_output_layout.addWidget(table_to_output)
            		#
            		table_to_log = BuildOutputForGUI(self, self.active, self.target)
            		#
            		# Add spacer and Command Specific table to Log tab
            		#
            		spacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Fixed)  # Adjust size as needed
            		self.tab_log_layout.addItem(spacer)
            		#
            		self.tab_log_layout.addWidget(table_to_log)
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by SGaist
            #5

            @Ed-Schneider
            I don't know what you are trying to achieve or what your issue is. A QTableWidget is derived from QTableView, so you can put the latter wherever you can put the former. You cannot put either, or any QWidget, in more than one place or on more than one tab. Nor can you directly copy any QWidget.

            A QTableWidget is essentially a QTableView with an internal model. You cannot share that model to another QTableWidget. However, if you use QTableViews you can at least share one model (your own) across them.

            I don't know what you mean/are trying to avoid in " I'm trying to avoid the rebuilding of the Widget". Depending on what you are up to, most work can or should be done in the model. So don't know what you are trying to copy that is an issue.

            E 1 Reply Last reply
            1
            • JonBJ JonB

              @Ed-Schneider
              I don't know what you are trying to achieve or what your issue is. A QTableWidget is derived from QTableView, so you can put the latter wherever you can put the former. You cannot put either, or any QWidget, in more than one place or on more than one tab. Nor can you directly copy any QWidget.

              A QTableWidget is essentially a QTableView with an internal model. You cannot share that model to another QTableWidget. However, if you use QTableViews you can at least share one model (your own) across them.

              I don't know what you mean/are trying to avoid in " I'm trying to avoid the rebuilding of the Widget". Depending on what you are up to, most work can or should be done in the model. So don't know what you are trying to copy that is an issue.

              E Offline
              E Offline
              Ed Schneider
              wrote on last edited by
              #6

              @JonB What I'm trying to avoid is unnecessary repetition. In a sense I want to build the table/model once and display it in two places. It should "look" the same in both places. Using TableView appears to satisfy that criterion. What I'm concerned about is whether what is put on the tabs is static or dynamic. Only one tab is impacted because it retains old content while the first one just shows current content. In a sense the tab I'm concerned about acts like a log of all past events. If it, what appears on the tab. is static, great, if it's dynamic I wonder whether I could use deepcopy to make a copy of the model. Is that a possibility?

              JonBJ JoeCFDJ 2 Replies Last reply
              0
              • E Ed Schneider

                @JonB What I'm trying to avoid is unnecessary repetition. In a sense I want to build the table/model once and display it in two places. It should "look" the same in both places. Using TableView appears to satisfy that criterion. What I'm concerned about is whether what is put on the tabs is static or dynamic. Only one tab is impacted because it retains old content while the first one just shows current content. In a sense the tab I'm concerned about acts like a log of all past events. If it, what appears on the tab. is static, great, if it's dynamic I wonder whether I could use deepcopy to make a copy of the model. Is that a possibility?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @Ed-Schneider
                For Qt you will supply your own model(s). You can have that support (deep) copying as you please. Qt does not dictate or support that, you just write your own code. You just need it to supply the interface required by QAbstractItemModel Class. That will allow QTableView to use it as its model.

                The important thing is to keep the model and the view distinct. If you have not already done so, (re-)read https://doc.qt.io/qt-6/model-view-programming.html.

                1 Reply Last reply
                0
                • E Ed Schneider

                  @JonB What I'm trying to avoid is unnecessary repetition. In a sense I want to build the table/model once and display it in two places. It should "look" the same in both places. Using TableView appears to satisfy that criterion. What I'm concerned about is whether what is put on the tabs is static or dynamic. Only one tab is impacted because it retains old content while the first one just shows current content. In a sense the tab I'm concerned about acts like a log of all past events. If it, what appears on the tab. is static, great, if it's dynamic I wonder whether I could use deepcopy to make a copy of the model. Is that a possibility?

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  @Ed-Schneider write an assignment operator in your model class
                  https://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)
                  and do a deep copy there.

                  SGaistS 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @Ed-Schneider write an assignment operator in your model class
                    https://en.wikipedia.org/wiki/Assignment_operator_(C%2B%2B)
                    and do a deep copy there.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JoeCFD I hope you are not suggesting to add an assignment operator to a QObject based class where it is explicitly stated that it is not copyable for good reasons.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JoeCFDJ 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      @JoeCFD I hope you are not suggesting to add an assignment operator to a QObject based class where it is explicitly stated that it is not copyable for good reasons.

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #10

                      @SGaist Good point. Thanks. This should be done in his own model class. He can even use QTableWidget if he creates his own model class.

                      SGaistS 1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @SGaist Good point. Thanks. This should be done in his own model class. He can even use QTableWidget if he creates his own model class.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @JoeCFD if something has a custom data structure, the combo would be QTableView + QAbstractTableModel subclass on top of the custom data structure.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

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