Qt focusObjectChanged example?

Qt focusObjectChanged example?

本文关键字:example focusObjectChanged Qt      更新时间:2023-10-16

我正在寻找一个示例,说明当接收到'focusObjectChanged'信号时,如何处理插槽接收到的QObject指针。我想知道如何识别获得焦点的对象?

文档指出指针指向的是焦点对象,但我怎么知道是哪个呢?

我试过在调试器中分析指针,但没有发现任何明显的东西。

您可以确定类和对象名称、小部件属性…:

QObject::connect(qApp, &QApplication::focusObjectChanged, [](QObject *obj){
if(obj) qWarning() << obj->metaObject()->className() << obj->objectName();
QWidget* widget = qobject_cast<QWidget*>(obj);
if(widget) qWarning() << widget->geometry(); // or other properties
BaseType* baseType = qobject_cast<BaseType*>(obj);
if(baseType) qWarning() << baseType->some_actions();
Type1* type1 = qobject_cast<Type1*>(obj);
if(Type1) qWarning() << type1->some_actions();
Type2* type2 = qobject_cast<Type2*>(obj);
if(Type2) qWarning() << type2->some_actions();
});

或者将指针与小部件指针进行比较,以找到您需要的小部件:

SomeClassSlot(QObject* obj) {
if(!obj) return;
if(obj == m_mainWindows) ....
if(obj->parent() == m_table) ...
MyClass myClass = qobject_cast<MyClass*>(obj);
if(myClass) myClass->some_method();
}