PDA

View Full Version : QLineEdit ignore key Qt:Key_Asterisk



davidovv
6th March 2017, 15:05
i have two separate widgets in one layout
first widget accepts keyboard shortcuts like this

QAction *fnQuantity = new QAction(this);
fnQuantity->setShortcut(Qt::Key_Asterisk);
connect(fnQuantity, SIGNAL(triggered()), this, SLOT(on_editQuantity_clicked()));
this->addAction(fnQuantity);
the second widget has some QLineEdit fields and radio buttons on form, some of them are disabled
If one of qLineEdit fields has focus, when i press asterisk on numpad it just types * character in lineedit
if i click on one of disabled fields, or radio butons, and then press * on keyboard the action in first form is executed
I tried with overriding keypressevent on qlineedit, and installing event filter in second form but all i get is that * sign is not typed into QLineEdit, and fnQuantity action is not executed
How can i ignore asterisk sign from line edit... and let other widgets process it...?

Lesiok
6th March 2017, 15:36
In the slot on_editQuantity_clicked() check if the widget has focus. If they do not do anything.

davidovv
6th March 2017, 15:47
i want on_editQuantity_clicked() to get executed even when the focus is on qlineedit on second form, but it is not executed, because qlineedit takes that key and appends * in its text property and ends there...

davidovv
10th March 2017, 10:35
I am still trying to figure this out. The same issue is how to ignore some keyboard events in QLineEdit
On first widget i have two buttons with shortcuts [ctrl+a] and [ctrl+d] and tableview. On second sibling widget there is qlineedit that usualy holds the focus and some radio buttons
if i press ctrl+d the button [ctrl+d] gets clicked
if i press ctrl+a the text in qlineedit gets selected (select all shortcut)

---------------|---------------------
| [ctrl+a] | --------------- |
| [ctrl+d] | | qlineedit | |
| | --------------- |
| table | radio buttons |
---------------|--------------------
i tried subclassing qlineedit and ignoring some key events, but all i get is that that key event is ignored, and not passed like ctrl+d is.
when i see that ctrl+d is passed that makes me think that it is possible to do the same with other keys, but i cant make it work
i also wanted to use up and down arrows while in qlineedit to navigate table on first widget.
Can i achieve this by just editing second widget event/eventfilter? How? What am i missing?

Santosh Reddy
10th March 2017, 11:48
I think you have installed the key action on the buttons, install the same action on the QLineEdit too.

davidovv
10th March 2017, 13:33
I don't quite understand what you mean, (btw i don't even understand your quote/signature message :) )
In my first attempt (first message) i created 2 qactions in first widget. Two widgets are complete separate files, loaded dynamically into one layout and have no knowledge of each other except that when second completes its job it sends signal to first, the second holds the focus.
In first widget created two actions that have shortcuts F2 and F3, when qlineedit has focus and i press F2 or F3, the actions are executed
Then i tried to change action shortcuts to asterisk (*) and slash (/) (Key_Asterisk and Key_Slash). If i have QLineEdit in focus actions are not executed but text is appended to lineedit, like it should.
But i want to create special case and for this lineedit and ignore those events just like F2 and F3 keys are ignored, and not just ignored but appropriate actions get executed.
I subclassed QLIneedit, changed its keyEvent, event.. i subclassed second widget and reimplemented eventFilter but all i get is that those keypresses are ignored, no text is appended to lineedit, but actions are not executed.
How can i make Asterisk key invisible for QLineEdit like F2 key is and
I also wanted to use shortcuts ctrl+a, up arrow, down arrow and ctrl+d. Those shortcuts are set on first widgets for buttons via qtdesigner (property manager), and only ctrl+d works while in qlineedit focus
if the focus is on radiobuttons on second widget all those shortcuts from first widget work, so the thing that those two widgets are siblings and that focus is on another widget is not the problem, right!?
What i have to do to my QLineEdit so it behaves the same way as radiobutton when in focus (concerning those shortcuts)
I think i have to subclass it (like i did) unregister something... but what

Santosh Reddy
10th March 2017, 14:01
Hope this helps


class LineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit LineEdit(QWidget * parent = 0) : QLineEdit(parent) { }

signals:
void shortcutDetected();

protected:
void keyPressEvent(QKeyEvent * event)
{
if(event->key() == Qt::Key_Asterisk)
{
event->accept();
return;
}

return QLineEdit::keyPressEvent(event);
}

void keyReleaseEvent(QKeyEvent * event)
{
if(event->key() == Qt::Key_Asterisk)
{
event->accept();
emit shortcutDetected();
return;
}
}
};