Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved qlineedit + qdialog, focus

    General and Desktop
    4
    9
    1247
    Loading More Posts
    • 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
      Anas_Deshmukh last edited by

      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 Reply Quote 0
      • A
        Anas_Deshmukh @Anas_Deshmukh last edited by

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

        jsulm 1 Reply Last reply Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @Anas_Deshmukh last edited by 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;
              }

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

          A 1 Reply Last reply Reply Quote 2
          • A
            Anas_Deshmukh @jsulm last edited by

            @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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 1
              • A
                Anas_Deshmukh @SGaist last edited by

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

                jsulm JonB 2 Replies Last reply Reply Quote 0
                • jsulm
                  jsulm Lifetime Qt Champion @Anas_Deshmukh last edited by

                  @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 Reply Quote 0
                  • JonB
                    JonB @Anas_Deshmukh last edited by JonB

                    @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 Reply Quote 1
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      @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 Reply Quote 1
                      • First post
                        Last post