Copying one table widget onto two tabs
-
@Ed-Schneider You should better use QTableView with a model.
-
@Ed-Schneider
In QtQWidget
s cannot be copied, and that extends up to yourQTableWidget
.Follow @jsulm's advice (separate
QTableView
s per tab, shared model). -
@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)
-
@Ed-Schneider
I don't know what you are trying to achieve or what your issue is. AQTableWidget
is derived fromQTableView
, so you can put the latter wherever you can put the former. You cannot put either, or anyQWidget
, in more than one place or on more than one tab. Nor can you directly copy anyQWidget
.A
QTableWidget
is essentially aQTableView
with an internal model. You cannot share that model to anotherQTableWidget
. However, if you useQTableView
s 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.
-
@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?
-
@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 allowQTableView
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.
-
@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.