Search code examples
qtqfiledialog

QFileDialog with virtual keyboard


Is there a way to detect when the filename text edit gets focus? I need to display a virtual keyboard when the text edit is entered

Thanks


Solution

  • That's not an exact solution, but here's a rather hacky way, leading to it, should work -- QFileDialog is also a widget, with children, so you can get access to that filename QLineEdit and do whatever needed with it.

    Something like... QLineEdit* lineEdit = dlg->findChild<QLineEdit*>(); Message filter would be better, because it would not require additional manipulations or change the dialog's behavior.

    QLineEdit* lineEdit = dlg->findChild<QLineEdit*>();
    FocusEater* filter = new FocusEater(this);
    lineEdit->installEventFilter(filter);
    
    connect(filter, &FocusEater::gotFocus, [](){
        QMessageBox::information(nullptr, "FUU", "BAR");
    });
    

    Still you'll have to mess a bit with correct event handling, tried myself on a simpliest demo, it worked:

    class FocusEater : public QObject
    {
        Q_OBJECT
    
    public:
        explicit FocusEater(QObject* parent)
            : QObject(parent)
        {}
    
    signals:
        void gotFocus();
    
    protected:
        bool eventFilter(QObject *obj, QEvent *event) override
        {
            if (event->type() == QEvent::FocusIn)
            {
                emit gotFocus();
                return true;
            }
            else
                return QObject::eventFilter(obj, event);
        }
    };
    

    Actually, it's not very difficult to implement a custom file dialog, depending on the volume of supposed customizations, but "focus detection" exactly is also possible.