QFileDialog with virtual keyboard

别说谁变了你拦得住时间么 提交于 2021-01-29 12:20:52

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/55020752/qfiledialog-with-virtual-keyboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!