赋料扬雄敌,诗看子建亲。这篇文章主要讲述如何在QML ApplicationWindow中获取activeFocusControl的className相关的知识,希望能为你提供帮助。
我试试
ApplicationWindow {
onActiveFocusControlChanged: {
console.log(activeFocusControl)
console.log(activeFocusControl.objectName)
}
}
输出:
qml: QQuickTextField(0xa6ec00)//the 'activeFocusControl'
qml://the 'activeFocusControl.objectName'
qml: QQuickButton(0xd7ccb0)
qml:
我想要
onActiveFocusControlChanged: {
if (activeFocusControl.className == "QQuickTextField") {
//do something
}
else if (activeFocusControl.className == "QQuickButton") {
//do something
}
但是“className”方法不存在,所以我怎么做呢?
对不起,我的英语是游泳池,谢谢
答案没有方法可以从qml访问className,因此可能的解决方案是从c ++创建一个帮助程序,如下所示:
#include <
QGuiApplication>
#include <
QQmlApplicationEngine>
#include <
QQmlContext>
#include <
QObject>
class Helper : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QString getClassName(QObject *obj) const{
return obj? obj->
metaObject()->
className(): "";
}
};
#include "main.moc"int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endifQGuiApplication app(argc, argv);
Helper helper;
QQmlApplicationEngine engine;
engine.rootContext()->
setContextProperty("helper", &
helper);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
【如何在QML ApplicationWindow中获取activeFocusControl的className】那么它在QML方面使用:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.3ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")Button {
id: button
x: 270
y: 47
text: qsTr("Button")
}TextField {
id: textField
x: 220
y: 169
text: qsTr("Text Field")
}onActiveFocusControlChanged:{
var className = helper.getClassName(activeFocusControl)
switch(className){
case "QQuickTextField":
console.log("I am QQuickTextField")
break
case "QQuickButton":
console.log("I am QQuickButton")
break
default:
console.log("empty")
}
}
}
完整的例子可以在以下link找到。
推荐阅读
- Android ListView performitemclick
- QML中的SVG未在Android上显示
- 如何阅读web.config文件中的appSettings部分()
- 在Azure App Service上启用gzip压缩
- web.config和app.config混淆
- web.config与.dll中的app.config
- appsettings文件属性是否覆盖了app.config中的内容()
- C#/ ASP.Net(app.settings而不是web.config)
- 使用Azure Web App上的web.config将子域重写到目录