I'm implementing a text-based to-do program. I have a CommandInput
widget which inherits from QLineEdit
. Basically there are a few commands, starting with keywords "add", "delete", "edit", and a few more.
I want to implement a few shortcuts.
Ctrl+A
so that when the widget has focus, I cansetText("add \"\"")
, cursor at the second last position, so it appears to beadd "|"
Tab
so that when the widget has focus, when the user types in the first keyword, for example,add
, then I can complete the command toadd "|"
The key problem is that when the widget has focus, the shortcuts are not working. I've tried the following ways:
1/ Override keyPressEvent
. The Tab
key does not work as intended. And even if it works, I don't know how to do that for a keySequence like Ctrl+A
.
void CommandInput::keyPressEvent(QKeyEvent *keyEvent)
{
switch(keyEvent->key())
{
case Qt::Key_Tab;
emit add_activated();
default:
QLineEdit::keyPressEvent(keyEvent);
}
}
2/ Create shortcuts when initializing. This doesn't work either.
CommandInput::CommandInput(QWidget *parent)
: QLineEdit(parent)
{
tab_shortcut = new QShortcut(QKeySequence("Tab"),this);
add_shortcut = new QShortcut(QKeySequence("Ctrl+A"),this);
connect(tab_shortcut, SIGNAL(activated()),
this, SIGNAL(tab_activated()));
connect(add_shortcut, SIGNAL(activated()),
this, SIGNAL(add_activated()));
}
Hope you could help me on this issue. Thanks!