Part1
测试代码,通过分析可以知道焦点变换过程和相关函数作用
- QLineEdit *lineedit;
- QWidget *currentitem;
- currentitem=QApplication::focusWidget();
- if(currentitem!=NULL)
- {
- if(currentitem->inherits("QLineEdit"))
- {
- qDebug()<<"shi QLineEdit";
- }
- else
- {
- qDebug()<<"bushi QLineEdit";
- }
- }
- else
- {
- qDebug()<<"current item is null!";
- }
- if(ui->lineEdit==currentitem)
- {
- qDebug()<<"ada~";
- lineedit=qobject_cast<QLineEdit *>(currentitem);
- lineedit->setText("dsaf");
- }
上面这段代码每隔一秒执行一次
输出结果如下:
刚开始启动
current item is null!
bushi QLineEdit
bushi QLineEdit
点击控件lineEdit后
shi QLineEdit
ada~
shi QLineEdit
ada~
点击另一个控件lineEdit_2后
shi QLineEdit
shi QLineEdit
shi QLineEdit
点击按钮后
bushi QLineEdit
bushi QLineEdit
由此看到,界面刚启动时候,当前焦点控件为NULL,之后迅速变为非空,但是并没有指向界面的输入框,当点击输入框的时候,当前焦点控件变为输入框,点击其他控件时焦点变为其他函数,当界面转到后台时,界面失去焦点。
Part2
代码作用是获取当前有效焦点控件,并判断是否为输入框,之后输入
- void SimuKey::slotofkey(char * keycode)
- {
- QLineEdit *lineedit;
- QWidget *currentitem;
- currentitem=QApplication::focusWidget();
- if(currentitem!=NULL)
- {
- if(currentitem->inherits("QLineEdit"))
- {
- lineedit=qobject_cast<QLineEdit *>(currentitem);
- //lineedit->setText(lineedit->text()+QString(keycode));
- lineedit->insert(QString(keycode));
- }
- else
- {
- qDebug()<<"bushi QLineEdit";
- }
- }
- }