QtVirtualKeyboard在QTableView单元格编辑时的焦点与触发问题
Let's tackle these two issues one by one—they're both rooted in focus management and how Qt Virtual Keyboard integrates with QTableView's editor widgets.
Issue 1: Cell loses focus when tapping virtual keyboard keys, with "InputContext::sendKeyClick(): no focus to send key click" error
This happens because the QQuickWidget hosting your virtual keyboard is stealing focus from the table cell's editor whenever you tap a key. The input context can't find a focused input control to send key events to, hence the error.
Fix Steps:
Prevent QQuickWidget from grabbing focus
When initializing theQQuickWidgetthat loads your virtual keyboard QML, set its focus policy to avoid competing with the cell editor:QQuickWidget *keyboardWidget = new QQuickWidget(this); keyboardWidget->setFocusPolicy(Qt::NoFocus); // Critical line to keep focus on the editor keyboardWidget->setSource(QUrl("qrc:/virtualkeyboard.qml")); // Add to your layout as usualDisable focus on the virtual keyboard QML component
In yourvirtualkeyboard.qml, ensure theInputPaneldoesn't request focus, so it never takes focus away from the cell editor:import QtQuick 2.15 import QtQuick.VirtualKeyboard 2.15 InputPanel { id: inputPanel anchors.fill: parent visible: inputMethod.visible focus: false // Don't let the keyboard claim focus }Force focus on the cell editor when created
Override yourQTableView'screateEditormethod to explicitly set focus on the editor, ensuring the input context tracks it correctly:QWidget *CustomTableView::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *editor = QTableView::createEditor(parent, option, index); if (editor) { editor->setFocus(); // Ensure the editor holds focus } return editor; }
Issue 2: Virtual keyboard only appears on double-click, not single-click (even with editTriggers = CurrentChanged)
Setting editTriggers to CurrentChanged enables single-click editing, but the virtual keyboard relies on detecting a focus change in an input widget to show up. Sometimes this focus transition isn't automatically picked up by the input context.
Fix Steps:
Manually trigger the keyboard when editing starts
Extend thecreateEditoroverride from Issue 1 to explicitly show the input method when the editor is created:QWidget *CustomTableView::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *editor = QTableView::createEditor(parent, option, index); if (editor) { editor->setFocus(); QGuiApplication::inputMethod()->show(); // Force the keyboard to appear } return editor; }Verify edit triggers configuration
Double-check that yourQTableView's edit triggers are correctly set (either in Qt Designer or code):ui->tableView->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::DoubleClicked);Ensure the QT_IM_MODULE variable is set early
Set the environment variable before initializingQApplicationinmain.cpp—system-level variables might not always propagate correctly to the app:int main(int argc, char *argv[]) { qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); // Set this first QApplication app(argc, argv); // Rest of your app initialization return app.exec(); }
Additional Checks
- Make sure your
.profile includes all required modules:QT += core gui widgets quickwidgets virtualkeyboard - Test the virtual keyboard with a simple
QLineEditfirst to confirm it works with basic input widgets—this helps rule out QTableView-specific conflicts.
内容的提问来源于stack exchange,提问作者Simpsons

