Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set focus to another qLineEdit by pressing the enter-key or by using arrow-keys

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    phil63
    wrote on last edited by
    #1

    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();@

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phil63
        wrote on last edited by
        #3

        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' to QWidget*'

        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;;
        

        }
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved