problem to edit cell in qtablewidget
Luca Bertolotti
luca72.bertolotti at gmail.com
Wed Sep 11 08:10:23 BST 2024
Hello, I have done in this way it works I don't know if it's correct but it
works:
I have add:
class Form(QWidget, Ui_Form):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget (defaults to None)
@type QWidget (optional)
"""
super().__init__(parent)
self.setupUi(self)
self.tableWidget.installEventFilter(self)
then I add a def
def eventFilter(self, source, event):
if source == self.tableWidget:
if event.type() == 51:
if event.key() == 67:
self.copied_cells =
sorted(self.tableWidget.selectedIndexes())
return True
if event.key() == 86:
r = self.tableWidget.currentRow() -
self.copied_cells[0].row()
c = self.tableWidget.currentColumn() -
self.copied_cells[0].column()
for cell in self.copied_cells:
self.tableWidget.setItem(cell.row() + r,
cell.column() + c, QTableWidgetItem(cell.data()))
return True
return super().eventFilter(source, event)
Thanks
Il giorno mar 10 set 2024 alle ore 16:34 Maurizio Berti <
maurizio.berti at gmail.com> ha scritto:
> 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.
> 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).
>
> 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).
> Just subclass QTableWidget and promote it in Designer.
>
> 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.
>
> Regards,
> MaurizioB
>
> Il giorno mar 10 set 2024 alle ore 12:30 Luca Bertolotti <
> luca72.bertolotti at gmail.com> ha scritto:
>
>> Hello
>> i have add this in my app
>> class Form(QWidget, Ui_Form):
>> """
>> Class documentation goes here.
>> """
>>
>> def __init__(self, parent=None):
>> """
>> Constructor
>>
>> @param parent reference to the parent widget (defaults to None)
>> @type QWidget (optional)
>> """
>> super().__init__(parent)
>> self.setupUi(self)
>>
>> self.tableWidget.keyPressEvent = self.copy_paste
>>
>> then I add a def
>> def copy_paste(self,event):
>> if event.key() == Qt.Key.Key_C and (event.modifiers() &
>> Qt.KeyboardModifier.ControlModifier):
>> self.copied_cells = sorted(self.tableWidget.selectedIndexes())
>> print(self.copied_cells)
>> if event.key() == Qt.Key.Key_V and (event.modifiers() &
>> Qt.KeyboardModifier.ControlModifier):
>> r = self.tableWidget.currentRow() - self.copied_cells[0].row()
>> c = self.tableWidget.currentColumn() -
>> self.copied_cells[0].column()
>> for cell in self.copied_cells:
>> self.tableWidget.setItem(cell.row() + r, cell.column() +
>> c, QTableWidgetItem(cell.data()))
>>
>>
>> The copy_paste work but I can't edit the cells what is wrong??
>>
>
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240911/652b5394/attachment-0001.htm>
More information about the PyQt
mailing list