Qt5 C++: Subclassing QSpinBox and keyPressEvent won't work - c++

I want to track when the enter/return key is pressed and then to focus on some other widget but if it's not pressed than spinbox should take normal action instead of anything else. I've subclassed QSpinBox and created protected void keyPressEvent(QKeyEvent *event). Inside it is this code:
void MytSpinBox::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return) {
qDebug() << "return pressed";
editingFinished();
} else {
qDebug() << "Other key";
event->accept();
}
}
This code should work, but it doesn't. I don't know what's wrong, but when I focus on promoted spinbox and try to change number in it, it just won't work, I only get the debug message printed. If I push enter/return key than it print debug message and focus on some other widget which I set in editingFinished().
What am I doing wrong?

If you need to invoke default processing of the event, you need to call base class implementation:
void MytSpinBox::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Return) {
qDebug() << "return pressed";
editingFinished();
} else {
QSpinBox::keyPressEvent(event);
}
}

Related

Qt: eventFilter does not recieve KeyPress when QCompleter is shown

I have an subclassed QPlainTextEdit called Editor. The editor contains an object used to suggest text, called a Suggester. The user can press a button from the Suggester to show a QCompleter, the activeCompleter.
I need to mess with the activeCompleter when KeyPress events come in from the Editor, so in Suggester, I create an eventFilter and install it to the passed in Editor instance.
When the activeCompleter is not shown, the eventFilter receives the KeyPress events as expected. However, when the activeComplter is shown via activeComplter->complete(), the eventFilter does not recieve KeyPress events (the editor behaves however).
I don't understand why this is. The QCompleter is not a widget. The Editor retains focus. I do not intercept events or keys anywhere else.
Why is my eventFilter not receiving KeyPress events?
relevant code...
Editor::Editor(QWidget *parent) : QPlainTextEdit(parent){
suggester = new Suggester(this);
}
Suggester::Suggester(Editor* editor){
this->editor = editor;
editor->installEventFilter(this);
}
bool Suggester::eventFilter(QObject *obj, QEvent *event){
qDebug()<< "event filter of type " << event->type() << " from " << obj;
return false;
}
It seems that your eventFilter member is incomplete. You have to filter the QEvent:KeyPress event as follows:
bool Suggester::eventFilter(QObject *obj, QEvent *event)
{
if (obj == textEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
} else {
// pass the event on to the parent class
return Suggester::eventFilter(obj, event);
}
}

QScrollBar on mouse hover

I want to change QScrollBar style on mouse hover. I have tried to get it working by adding eventFilter, but it doesn't work.
Code:
qApp->installEventFilter(this);
bool Test::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::Scroll) {
QScrollEvent *scrollEvent = static_cast<QScrollEvent*>(event);
if (scrollEvent->scrollState() == QScrollEvent::Enter) {
qDebug() << "Enter";
this->setStyleSheet("QScrollBar:vertical {width: 20px;}");
}
if (scrollEvent->scrollState() == QScrollEvent::Leave) {
qDebug() << "Leave";
this->setStyleSheet("QScrollBar:vertical {width: 12px;}");
}
}
return QObject::eventFilter(object, event);
}
How can I do this?
The correct events to handle in your event filter would actually be QEvent::Enter, and QEvent::Leave. QScrollEvent is used when scrolling actually occurs, that's why it was not triggered.
You can also probably directly use stylesheets with the :hover attribute.

Signals returnPressed() and editingFinished() does not work for QLineEdit

I am using editingFinished() signals of QLineEdit to perform an Operation. The documentation says that this signal will be emitted when return or enter key is pressed or when it will lose focus.
It works well with the enter key on the numlock (Windows keyboard), and also when it loses focus, but when i press "return key" on the keyboard, the signal is not emitted. i tried to use the returnPressed() signal, it behaves the same way.
Am i missing something ?
Thank you
Subclass QLineEdit
Reimplement keyPressEvent()
Catch Qt::Key_Enter pressing and do your job or emit signal yourself
From documentation:
Qt::Key_Return 0x01000004
Qt::Key_Enter 0x01000005 Typically located on the keypad.
Something like this:
void LineEdit::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Enter)
{
//do something
}
}
If you do not want subclass, you can installEventFilter to your dialog window, catch your lineEdit and check is Qt::Key_Enter was pressed.
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->lineEdit && event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if(Qt::Key_Enter == keyEvent->key() )
{
qDebug() << "numpad Enter pressed";
}
}
}
Don't forget
protected:
bool eventFilter(QObject *obj, QEvent *event);//in header
and
qApp->installEventFilter(this);//in constructor
For example:
void MainWindow::on_lineEdit_returnPressed()
{
qDebug() << "numpad Enter pressed";
}

Keyboard letters of a to z are not working in keyPressEvent in Qt

I am using KeyPressEvent in my applicatiion. But letters from a to z are not working.
void mywindow::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_Left:
break;
}
}
This is working properly
But when I'm using Key_R or Key_L it is not working.
Edit: keyReleaseEvent works with those letters.
Try this:
void SimpleWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_R)
{
// Key R was pressed
}
}
Or you can simply check key value using QString QKeyEvent::text () const method.
Not sure you still need it, but maybe someone will.
I was in the same situation, when pressing a to z letters none event was sent to my QMainWindow, but ctrl, cmd, alt or esc... was working.
I solved the problem adding this in the constructor.
this->setFocusPolicy ( Qt::StrongFocus );
read setFocusPolicy doc
then
MyQMainWindow::keyPressEvent(:keyPressEvent(QKeyEvent *pevent)
{
if (pevent->key() == Qt::Key_Control)
qDebug() << "ctrl pressed";
if (pevent->key() == Qt::Key_A)
qDebug() << "a pressed";
}

Qt input dialog - enter value without closing

I have QtInputDialog and I dont like it that, when i press enter it closes.
I would like to type value and confirm it by pressing enter on keyboard. After that line edit resets and i can type another value.
Dialog initialization:
void MainWindow::on_button1_clicked() {
dialog = new QInputDialog();
dialog->installEventFilter(this);
dialog->show();
}
Event filter:
bool MainWindow::eventFilter(QObject *o, QEvent *e) {
if (e->type() == QEvent::KeyPress) {
if (static_cast<QKeyEvent*>(e)->matches(QKeySequence::InsertParagraphSeparator)) {
qDebug() << dialog->textValue(); //use this value as you wish
dialog->setTextValue(QString());
return true; //block this event
}
}
return false;
}
Note that the dialog still can be closed using mouse click on "OK".