Re: Key Press Event trouble
You can subclass the window and override the keyPressEvent.
Or you can install filter on window to get its event...
QWidget::keyPressEvent
QObject::installEventFilter
Re: Key Press Event trouble
sorry but i looked at the docs and i cant make sense of it have yo please got a sample code to subclass the QMainWindow and add a keyPressEvent to it for QT:Key_Escape key
ive tried every thing but i cant get it to work
Thank you
Re: Key Press Event trouble
can we see wat u tried ? it shudne be a long code
Re: Key Press Event trouble
im doing this in python PTQT i have this code for the filter which is both an activity filter to reset a inactivity timer and a filter to react to the Escape key but its not working.
Code:
def __init__(self, parent=None):
super(activityFilter, self).__init__(parent)
self.eventaction = turnOffApp()
self.eventexit = MainDisplay()
def eventFilter(self, obj, event):
if event.
type() == QEvent.
KeyPress and event.
key() == Qt.
Key_Escape: self.eventexit.lockNow()
else:
self.eventaction.reset()
return QObject.
eventFilter(self, obj, event
)
Then at the bottom of the page i call the main Window an put this filter on it
Code:
def main():
app.setOrganizationName("random.")
app.setOrganizationDomain("random")
app.setApplicationName("random")
app.
setWindowIcon(QIcon(":/icon.png"))
filename = os.path.join(os.path.dirname(os.path.dirname(__file__)), "dataCache.db")
create
= not
QFile.
exists(filename
)
db.setDatabaseName(filename)
if not db.open():
QString("Database Error: %1").
arg(db.
lastError().
text())) sys.exit(1)
if create:
createDatabaseData()
app.processEvents()
form = MainWindow()
ef = activityFilter()
form.installEventFilter(ef)
form.show()
app.exec_()
main()
the filter calls to other class functions which are definitely working the problem lies in the filter
Re: Key Press Event trouble
this this...
8. def eventFilter(self, obj, event):
to
def keyPressEvent(self, event):
http://www.zetcode.com/tutorials/pyq...ntsandsignals/
section "Reimplementing event handler"
Re: Key Press Event trouble
Thanks for getting back to me that look slike a good tutorial you have linked there. i will look at that today.
Cheers!