Set focus to another qLineEdit by pressing the enter-key or by using arrow-keys



  • Hi,

    I am using several QLineEdit.
    And I would like to set the focus to the next QLineEdit when the down-arrow is pressed or when the Enter-key is pressed. And set the focus to the previous QLineEdit when the up-arrow is pressed.

    Could you help me to do so.

    Thank you in advance.

    @this->NumDonne1 = new QLineEdit(this);
    NumDonne1->setGeometry(210,53,45,26);
    //------------------------------
    this->NumDonne2 = new QLineEdit(this);
    NumDonne2->setGeometry(210,153,45,26);
    //------------------------------
    this->NumDonne3 = new QLineEdit(this);
    NumDonne3->setGeometry(210,253,45,26);
    //------------------------------
    this->NumDonne4 = new QLineEdit(this);
    NumDonne4->setGeometry(210, 353,45,26);
    //------------------------------

    NumDonne1->setFocus();@



  • Hi.
    For your goals you need
    @eventFilter(QObject *, QEvent *)@

    for conveniency create a line edit list:
    @QList<QObject*> leCollection;
    ...
    leCollection.append(NumDonne1);
    ...
    leCollection.append(NumDonneN);
    @

    first of all you need install event filter for each line edit:
    @
    foreach(QWidget *wdg, leCollection)
    {
    wdg - > installEventFilter(this)
    }@

    in eventFilter body:
    @bool MainWindow::eventFilter(QObject *obj, QEvent *pe)
    {
    if(pe->type() == QEvent::KeyPress)
    {
    int LeIndex = leCollection.indexOf(obj);
    QKeyEvent keyEvent = static_cast<QKeyEvent >(pe);
    if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter ||keyEvent->key() == Qt::Key_Down)
    {
    if(LeIndex>0)(static_cast<QWidget
    >(leCollection.at(LeIndex-1)))->setFocus();
    }
    if(keyEvent->key() == Qt::Key_Up)
    {
    if(LeIndex<leCollection.count()-1) (static_cast<QWidget
    >(leCollection.at(LeIndex+1)))->setFocus();
    }
    }
    return QMainWindow::eventFilter(obj, pe);
    }@

    something like that.



  • Hi, Thank you for your answer and valuable help.

    I followed your advises but I didn't succeed in adapting your code to my program.
    Please I still need your help.

    error message :
    fenetre.cpp:34: error: invalid conversion from QObject* const' toQWidget*'

    By changing :

    @foreach(QWidget *wdg, leCollection)@

    into

    @foreach(QObject *wdg, leCollection)@
    There is no error anymore , the program runs but no window shows up.

    Thank you
    @

    //fenetre.cpp
    //--------------------
    #include "fenetre.h"
    #include<QtGui>
    #include <QLineEdit>

    fenetre::fenetre()
    {

    this->NumDonne1 = new QLineEdit(this);
    // NumDonne1->setGeometry(210,53,45,26);
    //------------------------------
    this->NumDonne2 = new QLineEdit(this);
    NumDonne2->setGeometry(210,153,45,26);
    //------------------------------
    this->NumDonne3 = new QLineEdit(this);
    NumDonne3->setGeometry(210,253,45,26);
    //------------------------------
    this->NumDonne4 = new QLineEdit(this);
    NumDonne4->setGeometry(210, 353,45,26);
    //------------------------------

    // line->setFocus(Qt::keypressed()==Entrer);
    NumDonne3->setFocus();

    //QList<QObject*> leCollection;

    leCollection.append(NumDonne1);
    leCollection.append(NumDonne2);
    leCollection.append(NumDonne3);
    leCollection.append(NumDonne4);

    foreach(QWidget *wdg, leCollection)
    {
    wdg->installEventFilter(this);
    }

    //----------------------------
    }
    //---------------------------------------
    fenetre::~fenetre(){}
    //---------------------------------

    bool fenetre::eventFilter(QObject *obj, QEvent *pe)
    {
    if(pe->type() == QEvent::KeyPress)
    {
    int LeIndex = leCollection.indexOf(obj);
    QKeyEvent keyEvent = static_cast<QKeyEvent >(pe);
    if(keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter
    ||keyEvent->key() == Qt::Key_Down)
    {
    if(LeIndex>0)(static_cast<QWidget
    >(leCollection.at(LeIndex-1)))->setFocus
    ();
    }
    if(keyEvent->key() == Qt::Key_Up)
    {
    if(LeIndex<leCollection.count()-1)
    (static_cast<QWidget
    >(leCollection.at(LeIndex+1)))->setFocus();
    }
    }
    return fenetre::eventFilter(obj, pe);

    }

    @

    @
    //fenetre.h
    //--------------

    #ifndef FENETRE_H
    #define FENETRE_H
    #include <QtGui/QMainWindow>

    include <QLineEdit>

    #include <QLineEdit>

    class fenetre : public QWidget
    {
    Q_OBJECT
    public:

    fenetre();
    ~fenetre();

    QLineEdit *NumDonne1 ;
    QLineEdit *NumDonne2 ;
    QLineEdit *NumDonne3 ;
    QLineEdit *NumDonne4 ;

    QList<QObject*> leCollection;

    bool eventFilter(QObject *obj, QEvent *pe);

    public slots:

    };

    #endif // FENETRE_H

    @

    @
    //main.cpp
    //------------

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    fenetre h;
     h.setGeometry(100,100,300,300);
    
    h.show();
    return a.exec&#40;&#41;;
    

    }
    @



  • You can also check "focusNextChild()":http://qt-project.org/doc/qt-5.1/qtwidgets/qwidget.html#focusNextChild function. In this case you don't have to keep a list of widgets.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.