1

Basically, I just want to clear a text field iff the user has "clicked" the text field and the enter/return key has been pressed. After some research I found that you must first set the focusPolicy for each widget. Done.

 dataSend_area->setFocusPolicy(Qt::ClickFocus);

And from this point I am lost. From what I can tell, I will need to implement a slot function to implement a custom function for when these conditions are met. The pseudo-code presented below is about as far as I have gotten. I have searched through the Qt documentation and found little bits of information scattered about, but hardly any information regarding the coalescence of all of the functionality.

keyPressEvent(enter/return Key){
    if ( textBox has focus )
       //do
    else
        return 
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
sherrellbc
  • 4,650
  • 9
  • 48
  • 77

2 Answers2

1

You should override keyPressEvent function. These code may help.

void MainWidget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Return && my_text_widget->hasFocus()) {
         my_text_widget->setText("");
    }
}

Here is example how to override events http://programmingexamples.wikidot.com/qt-events

Ashot
  • 10,807
  • 14
  • 66
  • 117
1

Right, you got the concept more or less right. You indeed need the following actions:

1) Reimplement the key press event handler.

2) Check if it is the enter/return key that is pressed.

3) Check if the widget has the focus.

What you do not need to mess up with, however, is signal-slot what you mentioned. Events are slightly differently managed to signal-slots on your layer.

Your pseudo-code could be turned into real code this way:

#include <QKeyEvent>

...

void MyWidget::keyPressEvent(QKeyEvent *event)
{
    if (hasFocus())
        if(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
            clear();
}
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Is there a difference between the `enter` and `return` keys? Does it perhaps pertain to different platforms? – sherrellbc May 17 '14 at 18:48
  • 1
    @sherrellbc: enter is typically located on the keypad. See the following for details: http://qt-project.org/doc/qt-5/qt.html#Key-enum You asked for return and enter, so I assumed this is what you would mean. Some people prefer to use that enter, so IMHO it is better if the application supports that anyway, otherwise you will get complaints from your users quickly. :) – László Papp May 17 '14 at 18:48
  • I do not own a desktop, nor a laptop with the keypad so I was not aware. That is an interesting point though, I was under the impression that enter/return keys were interchangeable references to the same key. – sherrellbc May 17 '14 at 18:51
  • @sherrellbc: it depends on how you define them. In the Qt context, that seems to be the difference based on the official documentation. I personally think they are interchangable, so I would have named the item more descriptive personally. By the way, I think you will be able to get rid of the hasFocus() call since this method will be only called if you have the focus anyway, provided myWidget is the subclass of your text widget. – László Papp May 17 '14 at 18:52
  • I am not sure about your last statement. Perhaps we are not thinking about the same thing because I don't see any connection between a `keyPressEvent` and a `focusEvent`. – sherrellbc May 17 '14 at 19:41
  • @sherrellbc: the keyPressEvent will be only triggered if you have the focus, so there is no need check it, provided you subclass the text widget, which I think is the right way to do. – László Papp May 17 '14 at 19:45
  • Are you referring to the main widget (i.e. the main QMainWindow or QDialog) having focus? You are correct in that such signals will be fired only if the window has focus. I have a `QLineEdit` widget that I am doing this work on. If the main window is in focus and the user clicks enter I do not want anything to happen. However, if the user has clicked the QLineEdit box and then presses enter I want certain things to happen. If the hasFocus() call is removed, anytime the user hits enter with the global window open this event occurs and subsequently my function executes. – sherrellbc May 17 '14 at 19:49
  • First, you cannot "click" enter, you press it. Secondly, No, that is not correct. If you subclass the text widget as I already mentioned, the event will be only triggered if the line edit has the focus, again. – László Papp May 17 '14 at 19:57
  • @sherrellbc: if you need the focus and I misunderstand it, just add it. – László Papp May 17 '14 at 20:36
  • We may be at a mis-understanding then. That was my mistake for saying click rather than press. Also, I implemented the code with and without giving the QLineEdit focus. If you do not give focus to the QLineEdit and press enter without having the hasFocus() check, the function is executed. I could have likely implemented this in a non-intuitive manner and that could likely be the cause of our mis-understanding here. I have a QMainWindow that I attach and centralize a QWidget. The actual keyPressEvent overload is declared within the MainWindow class that inherits from QMainWindow. – sherrellbc May 18 '14 at 00:09
  • @sherrellbc: as my answer explains, you should implement this in the text widget, not main window. That is imho the wrong track for it. – László Papp May 19 '14 at 14:19
  • The QWidget contains the buttons and text areas. that same widget is then placed inside a QMainWindow object. – sherrellbc May 19 '14 at 14:32
  • @sherrellbc: that does not change the solution. You wish to handle the event within your text subclass, so do it there. You do not wish to handle it for the whole main window, so if you do so, you need the additional focus check, but if you implement it in the right place, you do not. Once you confirm that it works without hasFocus(), I will remove that from my answer. – László Papp May 19 '14 at 15:02