如何在Qt中获得当前关注的QLineEdit? [英] How can i get current focused QLineEdit in qt?

查看:364
本文介绍了如何在Qt中获得当前关注的QLineEdit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定qt当前焦点在哪个QLineEdit?

How can I identify which QLineEdit has the current focus in qt?

要为QLinEdit设置焦点,我已经尝试过:

To set the focus for QLinEdit I have tried:

   ui->linedit->setfocus();

但它对我也不起作用.我该如何解决这两个问题?

but it also not working for me. How can I solve these two?

推荐答案

要识别哪个焦点小部件(QlineEdit或任何QWidget),您需要获取所有当前的小部件子级,将每个子级转换为QLineEdit,并检查哪个子级具有焦点,示例代码:

To identify which focused Widget (QlineEdit or any QWidget), you need to get all your current widget children, cast each to QLineEdit, and check which one has focus, sample code:

QList<QWidget*> mylineEdits = this->findChildren<QWidget*>();
QListIterator<QWidget*> it(mylineEdits); // iterate through the list of widgets
QWidget *lineEditField;
while (it.hasNext()) {
    lineEditField = it.next(); // take each widget in the list
    if(QLineEdit *lineE = qobject_cast<QLineEdit*>(lineEditField)) {  // check if iterated widget is of type QLineEdit
        //
        if (lineE->hasFocus())
          {
            // this has the focus ...   
          }

    }
}

第二个问题,将重点放在QWidget上,已经在此帖子中得到了回答:

Second issue, setting focus on QWidget, already answered in this Post:

这篇关于如何在Qt中获得当前关注的QLineEdit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆