The import QtQuick 2.0
The import QtQuick. Controls 1.3
A Rectangle {
The function loadFocus () {
Edit. ForceActiveFocus ();
The console. The log (" test ");
}
Color: "blue";
Id: the test;
Focus: true,
({
The objectName: "edit";
Anchors. Left: the parent. Left;
Anchors. Top: parent. Top;
Width: 100;
Height: 100;
Id: edit;
Focus: true,
}
}
This is the input interface code, declare a method, let the external call, within method calls to edit, forceActiveFocus focus to make input box for
# include "inputwidget. H"
# include & lt; QVBoxLayout>
# include & lt; QPushButton>
# include & lt; QDebug>
# include & lt; QTextEdit>
# include & lt; QApplication>
# include & lt; QMouseEvent>
InputWidget: : InputWidget (QWidget * parent) : QWidget (parent)
{
M_pQuickView=new QQuickView ();
M_pQuickView - & gt; SetMinimumSize (QSize (200200));
M_pQuickView - & gt; SetClearBeforeRendering (false);
M_pQuickView - & gt; SetResizeMode (QQuickView: : SizeRootObjectToView);
M_pQuickView - & gt; SetSource (QStringLiteral (" QRC:/InputWidget. QML "));
QWidget * container=QWidget: : createWindowContainer (m_pQuickView, this);
The container - & gt; SetMinimumSize (QSize (200200));
QPushButton * BTN=new QPushButton ();
BTN - & gt; SetText (" test ");
The connect (BTN, SIGNAL (clicked (bool)), and this, SLOT (onClick ()));
QVBoxLayout * layout=new QVBoxLayout;
Layout - & gt; AddWidget (BTN);
Layout - & gt; AddWidget (container);
Layout - & gt; SetMargin (0);
SetLayout (layout);
}
Void InputWidget: : onClick () {
QObject * obj=(QObject *) m_pQuickView - & gt; RootObject ();
QMetaObject: : invokeMethod (obj, "loadFocus");
}
Is this widget code, add the QML input box, and create a button, when click the button to call the QML method, to set the focus,
But has been set up failure,
STH over and over again for a long time, did not solve, the feeling is you don't have to find some key points, such as when to invoke the QML code by c + + is that of the thread is give priority to the thread, and is in what condition, the input box is needed to set up successful,
At first I also simulated a mouse events to let the input box for focus, but still failed,
CodePudding user response:
Hang up one afternoon, not a single reply, because the problem is very low?CodePudding user response:
Sorry, turn to this, I am very don't understand the QML, I once met the focus is, however, I was found by traversing the reason, that is to say, I am all can get focus traversal of the current application of the component, who see the focus in the body and then find the reason, specific I remember is the qApp - & gt; CurrentFocus similar functionCodePudding user response:
Want to ask next the building Lord, how to implement the focus domain switching in the qML?CodePudding user response:
First call container - & gt; SetFocus () should can solve your problem