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. qlineedit + qdialog, focus
QtWS25 Last Chance

qlineedit + qdialog, focus

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.0k Views
  • 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.
  • A Offline
    A Offline
    Anas_Deshmukh
    wrote on last edited by
    #1

    hi all,
    i am posting my qt demo code, in my application i need to change focus when i press enter keys.

    enter key result in to close my dialox. i rectify this by installiling event filer but after that unable to change focus on enter press from qlineedit to next feild.

    //voucher.h
    
    #ifndef VOUCHER_H
    #define VOUCHER_H
    #include <QtCore>
    #include <QDialog>
    #include <QDialogButtonBox>
    #include <QFormLayout>
    #include <QDebug>
    #include <QLineEdit>
    #include <QComboBox>
    #include <QStringListModel>
    #include <QStringList>
    #include <QKeyEvent>
    
    class voucher: public QObject
    {
        Q_OBJECT
    public:
    
        QDialog *dialog=new QDialog;
    
        QLineEdit *voucherIdEdit = new QLineEdit(dialog);
        QLineEdit *voucherTypeLineEdit = new QLineEdit(dialog);
    
        QComboBox* catagoryListComboBox = new QComboBox();
    
        QComboBox* voucherNameComboBox = new QComboBox();
        QStringList voucherNameList;
        QStringListModel* voucherNameListModel = new QStringListModel(voucherNameList, this);
        QStringList catagoryList;
    
    
        bool voucherPopUp(QString str);
        Q_INVOKABLE bool eventFilter(QObject *obj, QEvent *event);
    
    signals:
       void sigVoucherIdEditFocusOut();
    
    public slots :
    //    void autoEnterTask(QString voucherCode);
        void manualEnterTask();
    };
    
    #endif // VOUCHER_H
    
    
    
    // voucher.cpp
    #include "voucher.h"
    
    
    bool voucher::voucherPopUp(QString str)
    {
        dialog->setFixedSize(500, 250);
        dialog->setWindowTitle("Voucher");
        dialog->setStyleSheet("QDialog {background-color: #D2D6DF;}");
        dialog->installEventFilter(this);
    
        QFormLayout form(dialog);
        QList<QLineEdit *> fields;
    
        QString voucherIdLabel = QString("Voucher Id : ");
        form.addRow(voucherIdLabel, voucherIdEdit);
        voucherIdEdit->setMaxLength(18);
        voucherIdEdit->setFocus();
        voucherIdEdit->clear();
    
    //    connect(voucherIdEdit, SIGNAL(returnPressed()), voucherIdEdit,SIGNAL(editingFinished()));
        connect(voucherIdEdit, SIGNAL(editingFinished()), this, SLOT(manualEnterTask()));
    
        fields << voucherIdEdit;
    
        QStringListModel* catagoryListModel = new QStringListModel(catagoryList, this);
        QString catagoryListLabel= QString("Category : ");
        catagoryListComboBox->setModel(catagoryListModel);
        form.addRow(catagoryListLabel, catagoryListComboBox);
    
        QStringList voucherNameList;
    
        QString voucherNameLabel= QString("Voucher Name : ");
        form.addRow(voucherNameLabel, voucherNameComboBox);
    
        voucherTypeLineEdit->setEnabled(false);
        voucherTypeLineEdit->clear();
    
        QString voucherTypeListLabel= QString("Type : ");
    
        form.addRow(voucherTypeListLabel, voucherTypeLineEdit);
    
        QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,  Qt::Horizontal, dialog);
        buttonBox.setStyleSheet("* { button-layout: 2 }");
    
        QObject::connect(&buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
        QObject::connect(&buttonBox, SIGNAL(rejected()),dialog, SLOT(reject()));
        form.addRow(&buttonBox);
    
        int ret=dialog->exec();
    
        if (ret == QDialog::Accepted) {
            voucherNameComboBox->disconnect();
            qDebug() << "Voucher : confirm click";
        }
        else if (ret == QDialog::Rejected) {
            qDebug() << "Voucher : cancel click";
        }
    }
    
    
    void voucher::manualEnterTask()
    {
        qDebug() << "voucher::manualEnterTask() slot get call from voucherIdLineEdit enter pressed.";
    
    }
    
    
    
    
    bool voucher::eventFilter(QObject *obj, QEvent *event) {
        if (event->type() == QEvent::KeyPress) {
    
             QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
    
             if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return) )
             {
                 qDebug() << "Enter pressed : voucher::eventFilter";
                 return true;
             }
        }
    }
    
    
    
    /// main.cpp
    ...
        voucher voucherObj;
        int retValu = voucherObj.voucherPopUp("voucher pop up.");
    ...
    
    
    A 1 Reply Last reply
    0
    • A Anas_Deshmukh

      hi all,
      i am posting my qt demo code, in my application i need to change focus when i press enter keys.

      enter key result in to close my dialox. i rectify this by installiling event filer but after that unable to change focus on enter press from qlineedit to next feild.

      //voucher.h
      
      #ifndef VOUCHER_H
      #define VOUCHER_H
      #include <QtCore>
      #include <QDialog>
      #include <QDialogButtonBox>
      #include <QFormLayout>
      #include <QDebug>
      #include <QLineEdit>
      #include <QComboBox>
      #include <QStringListModel>
      #include <QStringList>
      #include <QKeyEvent>
      
      class voucher: public QObject
      {
          Q_OBJECT
      public:
      
          QDialog *dialog=new QDialog;
      
          QLineEdit *voucherIdEdit = new QLineEdit(dialog);
          QLineEdit *voucherTypeLineEdit = new QLineEdit(dialog);
      
          QComboBox* catagoryListComboBox = new QComboBox();
      
          QComboBox* voucherNameComboBox = new QComboBox();
          QStringList voucherNameList;
          QStringListModel* voucherNameListModel = new QStringListModel(voucherNameList, this);
          QStringList catagoryList;
      
      
          bool voucherPopUp(QString str);
          Q_INVOKABLE bool eventFilter(QObject *obj, QEvent *event);
      
      signals:
         void sigVoucherIdEditFocusOut();
      
      public slots :
      //    void autoEnterTask(QString voucherCode);
          void manualEnterTask();
      };
      
      #endif // VOUCHER_H
      
      
      
      // voucher.cpp
      #include "voucher.h"
      
      
      bool voucher::voucherPopUp(QString str)
      {
          dialog->setFixedSize(500, 250);
          dialog->setWindowTitle("Voucher");
          dialog->setStyleSheet("QDialog {background-color: #D2D6DF;}");
          dialog->installEventFilter(this);
      
          QFormLayout form(dialog);
          QList<QLineEdit *> fields;
      
          QString voucherIdLabel = QString("Voucher Id : ");
          form.addRow(voucherIdLabel, voucherIdEdit);
          voucherIdEdit->setMaxLength(18);
          voucherIdEdit->setFocus();
          voucherIdEdit->clear();
      
      //    connect(voucherIdEdit, SIGNAL(returnPressed()), voucherIdEdit,SIGNAL(editingFinished()));
          connect(voucherIdEdit, SIGNAL(editingFinished()), this, SLOT(manualEnterTask()));
      
          fields << voucherIdEdit;
      
          QStringListModel* catagoryListModel = new QStringListModel(catagoryList, this);
          QString catagoryListLabel= QString("Category : ");
          catagoryListComboBox->setModel(catagoryListModel);
          form.addRow(catagoryListLabel, catagoryListComboBox);
      
          QStringList voucherNameList;
      
          QString voucherNameLabel= QString("Voucher Name : ");
          form.addRow(voucherNameLabel, voucherNameComboBox);
      
          voucherTypeLineEdit->setEnabled(false);
          voucherTypeLineEdit->clear();
      
          QString voucherTypeListLabel= QString("Type : ");
      
          form.addRow(voucherTypeListLabel, voucherTypeLineEdit);
      
          QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,  Qt::Horizontal, dialog);
          buttonBox.setStyleSheet("* { button-layout: 2 }");
      
          QObject::connect(&buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
          QObject::connect(&buttonBox, SIGNAL(rejected()),dialog, SLOT(reject()));
          form.addRow(&buttonBox);
      
          int ret=dialog->exec();
      
          if (ret == QDialog::Accepted) {
              voucherNameComboBox->disconnect();
              qDebug() << "Voucher : confirm click";
          }
          else if (ret == QDialog::Rejected) {
              qDebug() << "Voucher : cancel click";
          }
      }
      
      
      void voucher::manualEnterTask()
      {
          qDebug() << "voucher::manualEnterTask() slot get call from voucherIdLineEdit enter pressed.";
      
      }
      
      
      
      
      bool voucher::eventFilter(QObject *obj, QEvent *event) {
          if (event->type() == QEvent::KeyPress) {
      
               QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
      
               if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return) )
               {
                   qDebug() << "Enter pressed : voucher::eventFilter";
                   return true;
               }
          }
      }
      
      
      
      /// main.cpp
      ...
          voucher voucherObj;
          int retValu = voucherObj.voucherPopUp("voucher pop up.");
      ...
      
      
      A Offline
      A Offline
      Anas_Deshmukh
      wrote on last edited by
      #2

      @Anas_Deshmukh how i can make return key work as same as teb key !

      jsulmJ 1 Reply Last reply
      0
      • A Anas_Deshmukh

        @Anas_Deshmukh how i can make return key work as same as teb key !

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #3

        @Anas_Deshmukh

        if (event->type() == QEvent::KeyPress) {
            QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return) )
            {
                qDebug() << "Enter pressed : voucher::eventFilter";
                // Now post new key event (tab) using http://doc.qt.io/qt-5/qcoreapplication.html#postEvent
                return true;
            }

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        A 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Anas_Deshmukh

          if (event->type() == QEvent::KeyPress) {
              QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
              if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return) )
              {
                  qDebug() << "Enter pressed : voucher::eventFilter";
                  // Now post new key event (tab) using http://doc.qt.io/qt-5/qcoreapplication.html#postEvent
                  return true;
              }
          A Offline
          A Offline
          Anas_Deshmukh
          wrote on last edited by
          #4

          @jsulm thank you for ur response, but still i am bit confused on my approach. i will soon update you once i get my concept clear. thank you for your time.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            On a side note, in your event filter you only handle the case that interests you, all the others are dropped which means that they are not handled properly. See the documentation of the method.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              On a side note, in your event filter you only handle the case that interests you, all the others are dropped which means that they are not handled properly. See the documentation of the method.

              A Offline
              A Offline
              Anas_Deshmukh
              wrote on last edited by
              #6

              @SGaist thats what i am trying to do, treating my enter press event as tab event. thank you for links. i will update soon. :)

              jsulmJ JonBJ 2 Replies Last reply
              0
              • A Anas_Deshmukh

                @SGaist thats what i am trying to do, treating my enter press event as tab event. thank you for links. i will update soon. :)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Anas_Deshmukh But what about other events? Shouldn't they still work?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • A Anas_Deshmukh

                  @SGaist thats what i am trying to do, treating my enter press event as tab event. thank you for links. i will update soon. :)

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Anas_Deshmukh
                  What @SGaist and @jsulm are saying is: after all the other code you write in voucher::eventFilter(), you should end it with an unconditional line: return false;. That allows the event filter to work correctly when it is not your Enter key case.

                  Example at http://doc.qt.io/qt-5/eventsandfilters.html, Event Filters sub-topic.

                  @SGaist: in the example I quote they use return false;; in yours they use return <base-class>::eventFilter(). Does it make any difference which one to use where?

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JonB I'd say it will depend on whether your event filter class might be subclassed or not.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

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