0

I'm certain this or similar has been asked here many times. I searched for it, but could not make the answers work.

During editing, I use a form which primary aim is to reduce mouse interaction during edit. A attribute value can be modified (using a QPlainTextEdit widget)

Now, after updating the value, I need to click a button on the form to trigger an update-script.

It would be a real time-saver if, upon entering the value, the return keypress would trigger that update-script.

In other words: I need to catch the return keypress on that widget.

internet searches and searches here offer this answer. code-excerpt:

from qgis.PyQt import uic
frm = uic.loadUi( 'F_ChkPt.ui')

##  code snipped

def cmd_bt_chk():
    #update script 
    c_layer = frm.bt_active.text()
    layer = QgsProject.instance().mapLayersByName( c_layer )[0]
    
    n_pt = int(frm.tb_pt.toPlainText())
    
    feature = layer.getFeature( n_pt)
    feature[ c_VELDCHK] = '1'
    feature[ c_VELD] = frm.tb_Hg.toPlainText()
    layer.updateFeature(feature)
    cmd_bt_next()
     
class CustomPlainTextEdit(QPlainTextEdit):
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            cmd_bt_chk()               # update script
        super().keyPressEvent(event)        
        
#----------------------------------------------------------------
frm.bt_prev.clicked.connect( cmd_bt_prev)
frm.bt_prev.setShortcut("b")
frm.bt_next.clicked.connect( cmd_bt_next)
frm.bt_next.setShortcut("n")
frm.bt_chk.clicked.connect( cmd_bt_chk)
frm.bt_chk.setShortcut("m")

frm.tb_Hg = CustomPlainTextEdit()         # << add the keypress to the widget ??

frm.bt_active.clicked.connect( cmd_bt_SetActiveLayer)

frm.show()

This does nothing & no error is raised I do not understand what 'frm.tb_Hg = CustomPlainTextEdit()' is supposed to accomplish

Can somebody point me in the right direction?

1 Answer 1

0

Assuming frm is a QWidget or QMainWindow, you should add new Widgets to it using a layout and its 'addWidget'-Method I tried your CustomPlainTextEdit and it works for me in the following example:

from qgis.utils import iface 

class CustomPlainTextEdit(QPlainTextEdit):
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            print('hi')
        super().keyPressEvent(event) 
        
        
form = QMainWindow(iface.mainWindow())
central_widget = QWidget()
layout = QVBoxLayout(central_widget)
label = CustomPlainTextEdit()

form.setCentralWidget(central_widget)
layout.addWidget(label)

form.show()
New contributor
juliustoth is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.