The script works incorrectly with modal windows (exec()) because of focus loss or interruptions in the keyboard script. The keyboard logic is divided into two scripts: a focusLogic script and the keyboard script itself. focusLogic script:
#include "flyvirtualkeyboard.h"
#include "topo/customlineedit.h"
#include <QDebug>
#include <QBool>
#include <QTreeWidget>
#include <typeinfo>
FlyVirtualKeyboard::FlyVirtualKeyboard() {
keyboard = new Keyboard();
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
}
FlyVirtualKeyboard::~FlyVirtualKeyboard() {
disconnect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(focusChanged(QWidget*, QWidget*)));
delete keyboard;
}
void FlyVirtualKeyboard::close_keyboard() {
if (keyboard->isHidden())
return;
keyboard->hide();
}
void FlyVirtualKeyboard::focusChanged(QWidget *old, QWidget *now)
{
// Check if the new focus widget is part of the keyboard
keyboard->focusWidget = now;
if (now) {
qDebug() << "Focused widget:" << now->objectName() << "Type:" << typeid(*now).name();
} else {
qDebug() << "Focused widget: None";
}
if (now && (now == keyboard || keyboard->isAncestorOf(now)))
{
lineEdit->clearFocus();
//lineEdit->parentWidget()->setFocus();
return; // Ignore focus changes within the keyboard itself
}
if (checkWidgetType(now))
{
lineEdit = qobject_cast<QLineEdit*>(now);
if (lineEdit)
{
keyboard->setLineEdit(lineEdit);
keyboard->show();
qDebug() << "Keyboard shown";
}
}
else
{
keyboard->hide();
qDebug() << "Keyboard hidden";
}
}
bool FlyVirtualKeyboard::checkWidgetType(QWidget *obj) {
if (qobject_cast<QLineEdit*>(obj)) return true;
if (qobject_cast<QTextEdit*>(obj)) return true;
if (qobject_cast<QPlainTextEdit*>(obj)) return true;
if (qobject_cast<QSpinBox*>(obj)) return true;
if (qobject_cast<QDoubleSpinBox*>(obj)) return true;
if (qobject_cast<QTreeWidget*>(obj)) return true;
if (qobject_cast<customlineedit*>(obj)) return true;
//if (qobject_cast<Q3MultiLineEdit*>(obj) && !qobject_cast<Q3MultiLineEdit*>(obj)) return true;
return false;
}
To address the issue of the keyboard focus not working correctly with modal windows, we need to ensure that focus changes within modal windows are handled appropriately. The problem likely arises because modal windows can have their own focus management, and the current implementation might not account for that.
Евгений is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2