qlineedit + qdialog, focus



  • 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.");
    ...
    
    


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


  • Moderators

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


  • @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.


  • Lifetime Qt Champion

    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.



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


  • Moderators

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



  • @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?


  • Lifetime Qt Champion

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


Log in to reply
 

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