QT获取控件焦点,判断对象类型,模拟发送按键消息
2017-05-18 09:56
2026 查看
代码如下:
Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.
The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro.
A class is considered to inherit itself.
如果给定的object是T类型或者T的子类,那么该方法把object转换成T类型,否则返回0,空对象也返回0.
必要条件:要求T类型必须是继承自QObject 的对象
下面这个也可以用于判断
bool QObject::inherits(const char *className) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
A class is considered to inherit itself.
如果需要转换成T类,就用上面那个方法,如果只是判断用下面这个就可以。
pushButton要想接收默认键盘消息如Key_Return之类的,要勾选上其pushButton的default,autodefault属性
注意:
如果不设置QApplication::focusObject(),就会不正常。像一些方向键就不对了。
当需要在QEditline之类的输入框中模拟输入字符时,QKeyEvent还要加上const QString &text参数再发送,
使用postEvent时必须是
void MainWindow::keyPressEvent(QKeyEvent *event){ if(event->type()==QKeyEvent::KeyPress) { if(event->key()==Qt::Key_Return) { QWidget *current_focus_w = this->focusWidget();//获取当前焦点的控件 if(!current_focus_w) return; QPushButton * btn = qobject_cast<QPushButton*>(current_focus_w); if(btn) btn->click(); } } }
判断对象类型
T qobject_cast(QObject *object)Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.
The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro.
A class is considered to inherit itself.
如果给定的object是T类型或者T的子类,那么该方法把object转换成T类型,否则返回0,空对象也返回0.
必要条件:要求T类型必须是继承自QObject 的对象
下面这个也可以用于判断
bool QObject::inherits(const char *className) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
A class is considered to inherit itself.
如果需要转换成T类,就用上面那个方法,如果只是判断用下面这个就可以。
pushButton要想接收默认键盘消息如Key_Return之类的,要勾选上其pushButton的default,autodefault属性
模拟键盘发送消息
QKeyEvent *key_event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier,QString(QChar(key))); QCoreApplication::postEvent(QApplication::focusObject(), key_event);
注意:
如果不设置QApplication::focusObject(),就会不正常。像一些方向键就不对了。
当需要在QEditline之类的输入框中模拟输入字符时,QKeyEvent还要加上const QString &text参数再发送,
使用postEvent时必须是
QKeyEvent *类型变量,如果是sendEvent则可以
QKeyEvent类型变量。因为如果用的是局部变量,postEvent可能还没操作完局部变量就失效了,导致失败。
相关文章推荐
- 关于如何获取当前焦点控件并判断控件类型
- 关于如何获取当前焦点控件并判断控件类型
- 模拟向特定窗口或控件发送键盘消息(F1)
- android发送模拟按键消息,出现死锁,timeout的解决方法
- 在C#程序中模拟发送键盘按键消息
- 向指定窗口发送模拟键盘按键消息
- js,onblur后下一个控件获取焦点判断、html当前活跃控件、jquery版本查看、jquery查看浏览器版本、setTimeout&setInterval
- 用ENTER键切换控件焦点,并模拟按键
- 获取对象类型及类型判断的几种方法
- 判断焦点所在控件类型
- 详解Windows消息分类以及WM_COMMAND与WM_NOTIFY的区别,以及模拟发送控件通知消息
- 五.OC基础--1.多态,2.类对象,3.点语法,4.@property&@synthesize,5.动态类型,内省(判断对象是否遵循特定的协议,以及是否可以响应特定的消息)
- 在C#程序中模拟发送键盘按键消息
- 【Android应用开发】-(20)简单的模拟按键发送消息
- QT 判断当前焦点是否在某控件上
- 用ENTER键切换控件焦点,并模拟按键
- QT 4.7 控件间 互相发送消息例子
- 使用SendMessage方法对窗体上的控件进行截图,该方法的思想就是把控件的句柄拿到,对控件发送WM_PAINT消息,并且把希望得到图形对象的句柄当作wParam参数传过去,这样就可以在图形对象得到想要得图形。
- Windows消息(二):消息的分类以及模拟发送控件通知消息
- 模拟按键发送消息