<div dir="ltr">Hello, I have done in this way it works I don't know if it's correct but it works:<div>I have add:</div><div><div>class Form(QWidget, Ui_Form):<br> """<br> Class documentation goes here.<br> """<br><br> def __init__(self, parent=None):<br> """<br> Constructor<br><br> @param parent reference to the parent widget (defaults to None)<br> @type QWidget (optional)<br> """<br> super().__init__(parent)<br> self.setupUi(self)<br></div><div><br></div><div> self.tableWidget.installEventFilter(self)<br></div><div><br></div><div>then I add a def</div><div> def eventFilter(self, source, event):</div> if source == self.tableWidget:<br> if event.type() == 51:<br> if event.key() == 67:<br> self.copied_cells = sorted(self.tableWidget.selectedIndexes())<br> return True<br> if event.key() == 86:<br> r = self.tableWidget.currentRow() - self.copied_cells[0].row()<br> c = self.tableWidget.currentColumn() - self.copied_cells[0].column()<br> for cell in self.copied_cells:<br> self.tableWidget.setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))<br> return True<br> return super().eventFilter(source, event)</div><div><br></div><div>Thanks<br><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 10 set 2024 alle ore 16:34 Maurizio Berti <<a href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>It's not really clear what you meant by "can't edit" (begin editing of a cell? by clicking or by pressing an edit trigger key? actually typing/editing values in the editor?) but, in any case, the most important problem is that you're not calling the default implementation, which is used, among other things, to begin the editing of a cell while using the keyboard.</div><div>In proper conditions, you should be able to do it with super(), but you monkey patched the event handler in another class, and that won't work, so the only solution would be to call QTableWidget.keyPressEvent(self.tableWidget, event).</div><div><br></div><div>Monkey patching should be done only on rare occasions and with full awareness, but should be generally avoided for event handlers or any function that needs access to the super class if patching a different object like in this case. A more appropriate approach is to either use an event filter, or, even better, create a subclass (with a promoted widget). When using scroll areas, the latter is normally preferable, as QAbstractScrollArea subclasses internally "reroute" events of the viewport to the default QWidget event handlers, but that won't be exactly the same with an event filter (even if installed for the viewport).</div><div>Just subclass QTableWidget and promote it in Designer.<br></div><div><br></div><div>Note that the second if should actually be an elif (followed by a final else that calls the default implementation), and if copied_cell is not declared beforehand (eg. in the init) *and* its contents validated (before trying to get any item), that will raise an exception.</div><div><br></div><div>Regards,</div><div>MaurizioB<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 10 set 2024 alle ore 12:30 Luca Bertolotti <<a href="mailto:luca72.bertolotti@gmail.com" target="_blank">luca72.bertolotti@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello<div>i have add this in my app</div><div>class Form(QWidget, Ui_Form):<br> """<br> Class documentation goes here.<br> """<br><br> def __init__(self, parent=None):<br> """<br> Constructor<br><br> @param parent reference to the parent widget (defaults to None)<br> @type QWidget (optional)<br> """<br> super().__init__(parent)<br> self.setupUi(self)<br></div><div><br></div><div> self.tableWidget.keyPressEvent = self.copy_paste<br></div><div><br></div><div>then I add a def</div><div> def copy_paste(self,event):</div><div> if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):</div> self.copied_cells = sorted(self.tableWidget.selectedIndexes())<br> print(self.copied_cells)<br> if event.key() == Qt.Key.Key_V and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):<br> r = self.tableWidget.currentRow() - self.copied_cells[0].row()<br> c = self.tableWidget.currentColumn() - self.copied_cells[0].column()<br> for cell in self.copied_cells:<br> self.tableWidget.setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))<div><br></div><div><br></div><div>The copy_paste work but I can't edit the cells what is wrong??</div></div>
</blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>
</blockquote></div>